JS基礎知識復習(一)
6118 ワード
1.JS変数:
Title
/**
* JS :
* 1.
* 1.1 :number
* 1.2 :String
* 1.3 :boolean
* 1.4 :null undefined
* 2.
*/
var age = 18 , name = " " , sex = " ";
alert(" =" + name + ", :" + sex + ", :" + age);
alert(typeof name);
for (let i = 0; i < 20; i++) {
if (i % 5 == 0 && i != 0){
break;
}
document.write(i + ";");
}
/**
* input :
* document , JS input , 。
*/
var _input = document.getElementById("demo");
alert(_input.name);
/**
* : jQuery 。
*/
window.onload = function (){
_input = document.getElementById("demo");
alert(_input.name);
};
2.関数:
Title
/**
* arguments: ,
*/
function sum() {
// alert(arguments);
let p = arguments;
alert(p.length);
//
let arr = new Array(20);
alert(arr instanceof Array);
// arr.push("h");
// alert(arr.length);
}
sum(1,2,3);
/**
*
*/
function mySort(p , mode) {
// let newArray;
//
if (p instanceof Array){
if (mode == "" || mode == null || mode == undefined || mode == "asc"){
for (let i = 0; i < p.length; i++) {
let tmp ;
for (let j = i + 1; j < p.length; j++) {
if (p[i] > p[j]){
tmp = p[i];
p[i] = p[j];
p[j] = tmp;
}
}
}
}else if (mode == "desc"){
for (let i = 0; i < p.length; i++) {
let tmp ;
for (let j = i + 1; j < p.length; j++) {
if (p[i] < p[j]){
tmp = p[i];
p[i] = p[j];
p[j] = tmp;
}
}
}
}
}
return p;
}
var demo = [4,5,8,1,9 ,5 ,0 , 10 , 100 , 20 , 31];
demo = mySort(demo , '');
alert(demo);
3.配列常用関数:
Title
var arr = [10,20,30];
var strArr = ["123" , "hello"];
/**
* forEach:
*/
strArr.forEach(function (value , index , array) {
alert(" :" + index + "; = " + value + "; = " + array);
});
/**
* map: - -
*/
var newArr = arr.map(function (value, index, array) {
return value + 2;
});
alert(" = " + arr + " = " + newArr);
/**
* filter:
* @type {number[]}
*/
var arrFilter = arr.filter(function (value , index , array) {
return value >20;
});
alert(" = " + arrFilter);
/**
* some: , return , true, false
* :some , , true, !
*/
var arrCheck = arr.some(function (value, index, array) {
return value == 20;
});
alert(arrCheck);
/**
* every: some , return , true。 , false,
*/
var arrEvery = arr.every(function (value, index, array) {
return value > 10;
});
alert("arr 10 ?" + arrEvery);
4.Mathオブジェクト:
Title
/**
* Math
*/
/**
* Math.round:
*/
alert(Math.round(3.521313));
/**
* Math.random:0~1
*/
alert(Math.random());
alert(Math.max(1,2,10,100)); // ,min:
alert(Math.abs(-1)); //
alert(Math.ceil(3.1545)); //
alert(Math.floor(3.1545)); //
/**
* Math.pow(x , y): X y
*/
alert(Math.pow(2,3));
alert(Math.sqrt(4)); //
/**
* Math :sin、cos、tan、
* : 。Math.PI = 180 。 1 = Math.PI / 180
*/
alert(Math.sin(30 * Math.PI / 180));
alert(Math.cos(60 * Math.PI / 180));
5.オブジェクト:
Title
/**
*
*/
// 1. new
var obj = new Object();
obj.name = " ";
obj.age = 18;
obj.getName = function () {
return this.name;
};
alert(obj.getName());
// 2. new
// 3.
var _obj = {};
_obj.name = " ";
_obj.age = 20;
_obj.setName = function (p) {
this.name = p;
};
_obj.getName = function () {
return this.name;
}
_obj.setName(" ");
alert(_obj.getName());
// delete
delete _obj.age;
alert(_obj.age);