JS對象、包裝類
屬性的增、刪、改、查
增加屬性:ojb.newProp = “what";
刪除屬性:delete ojb.Prop
對象的創建方法
- var obj = {} plainObject 對象字面量/對象直接量
- 構造函數
1)系統自帶的構造函數 new Object()
2)自定義構造函數。
構造函數結構上和函數沒有任何區別:function person(name,sge){ //this = {}; this.name = name;//屬性 this.age = age; this.study = function(){ 函數體 }//方法 //return this; } var person1 = new person(name,age);
構造函數內部原理:
1.在函數體前面隱式地加上this={};
2.執行this.xxx=xxx;
3.隱式地返回this。
包裝類
- Boolean
- String
- Number
原始值不能有屬性和方法,當給它們添加屬性的時候系統會自動進行包裝類並銷毀。
var num = new Number(3);
var str = new String("chen");
var num = 3;
num.len = 2;
//new Number(3).len =2; delete
console.log(num.len);//endefined
var str = "abcd";
str.length = 2;
//new String('abcd).length = 2; delete;
console.log(str);//abcd
//new String('abcd).length
//.length是string系統自帶的屬性。
console.log(str.length);//4