1. JavaScriptオブジェクト
1.1オブジェクトを作成するには
ビルトインオブジェクトを作成する1.1.1オリジナルの方法
var myObject = new Object();
myObject.name = "lijie";
myObject.age = 20;
myObject.say = function(){...}
1.1.2直接カスタムオブジェクトを作成します
var 对象名 = {属性名1: 属性值, 属性名2: 属性值2, ...}
1.1.3は、カスタムコンストラクタを使用してオブジェクトを作成します
function pen(name, color, price){
// 1. 对象的 name 属性
this.name = name;
// 2. 对象的 color 属性
this.color = color;
// 3. 对象的 piece 属性
this.price = price;
// 4. 对象的 say 方法
this.say = function(){};
}
var pen = new pen("铅笔", "红色", 20);
pen.say();
1.2このキーワード
this
その意図は、言葉であります这个
- オブジェクトがメソッドを呼び出すと、このメソッドは、オブジェクトを表し
- 同様のPythonで
self
1.3トラバーサル
- 用-に声明を超えるすべての要素またはオブジェクトの配列のすべての属性を反復することができます
for(var i in window){
document.write(i + "---" + window[i]);
}
約1.4タイプ
// 1. global 对象的其中一个方法
typeof()
// 2. 查看当前对象的构造函数是谁
对象.constructor;
// 3. 数组推荐用这种方法,因为 typeof() 得到是 object
if(arr.constructor == Array){
alert("数组");
}
2. JavaScriptの配列
- 配列は、データの集まりです
- JS、データは異なるタイプであってもよい配列
メソッド定義アレイ2.1
// 1. 用对象的实例创建
var aList1 = new Array(1, 2, 3);
// 2. 直接量创建
var aList2 = [1, 2, 3, "abc"];
アレイの動作データの2.2方法
2.2.1配列の取得した長さ
var aList = [1, 2, 3, 4];
alert(aList.length); // alert 4
データ配列添字付き2.2.2
var aList = [1, 2, 3, 4];
alert(aList[0]); // alert 1
配列の欠失メンバーの終わりに2.2.3
var aList = [1, 2, 3, 4];
aList.push(5);
alert(aList); // alert 1, 2, 3, 4, 5
aList.pop();
alert(aList); // alert 1, 2, 3, 4
ヘッドアレイメンバーで2.2.4の追加および削除
var aList = [1, 2, 3, 4];
aList.unshift(5);
alert(aList); // alert 5, 1, 2, 3, 4
aList.shift();
alert(aList); // alert 1, 2, 3, 4
配列内のメンバーを追加または削除する2.2.5
var aList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
// 1. 若 splice() 只接受 1 个参数,表示删除该下标至末尾的所有元素,包括该下标
aList.splice(11);
console.log("1. aList =", aList);
// 2. 若 splice() 只接受 2 个参数,表示删除该两个下标间的所有元素,包括该下标
aList.splice(1, 3);
console.log("2. aList =", aList);
// 3. 若 splice() 只接受 3 个参数,前 2 个意思同上,第 3 个元素用来填补删除处
aList.splice(1, 3, 100);
console.log("3. aList =", aList);
// 4. 若 splice() 接受 3 个以上参数,前 3 个意思同上,第 4 个及之后的元素也用来填补删除处
aList.splice(1, 3, 101, 102, 103, 104);
console.log("4. aList =", aList);
1. aList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2. aList = [0, 4, 5, 6, 7, 8, 9, 10]
3. aList = [0, 100, 7, 8, 9, 10]
4. aList = [0, 101, 102, 103, 104, 9, 10]
2.3多次元配列
- 多次元アレイは、アレイを指すアレイのメンバーの配列であります
var aList = [[1, 2, 3], ['a', 'b', 'c']];
alert(aList[0][1]); // alert 2
3. Javascriptを数学的対象
3.1数学
// 1. 四舍五入
var res1 = Math.round(5.921);
var res2 = Math.round(5.321);
console.log("res1 =", res1);
console.log("res2 =", res2);
// 2.1 获取最大值
var res3 = Math.max(10, 20, 30, 41, 52, 14, 28, 39);
console.log("res3 =", res3);
// 2.2 获取最小值
var res4 = Math.min(10, 20, 30, 41, 52, 14, 28, 39);
console.log("res4 =", res4);
// 3. 获取绝对值
var res5 = Math.abs(-100);
console.log("res5 =", res5);
// 4.1 退一取整(向下取整)
var res6 = Math.floor(1.1);
var res7 = Math.floor(1.9);
console.log("res6 =", res6);
console.log("res7 =", res7);
// 4.2 进一取整(向上取整)
var res8 = Math.ceil(1.1);
var res9 = Math.ceil(1.9);
console.log("res8 =", res8);
console.log("res9 =", res9);
// 5. 幂运算
var res10 = Math.pow(2, 3);
console.log("res10 =", res10);
// 6. 开方运算
var res11 = Math.sqrt(9);
console.log("res11 =", res11);
res1 = 6
res2 = 5
res3 = 52
res4 = 10
res5 = 100
res6 = 1
res7 = 1
res8 = 2
res9 = 2
res10 = 8
res11 = 3
3.2ランダム()
- 乱数取得、の間のランダムな小数(0-1を返します]
- 確率論、確率の観点から、1にかかる場合がありますが、0に運ばれました
// 手动封装函数
function rand(m, n){
return Math.floor(Math.random() * (n-m+1)) + m; // m 写到 floor() 中也行
}
var res = rand(20, 30);