精品伊人久久大香线蕉,开心久久婷婷综合中文字幕,杏田冲梨,人妻无码aⅴ不卡中文字幕

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
javascript 創建對象的幾種方式
1、通過new Object創建
創建自定義對象的最簡單方式就是創建一個Object 的實例,然后再為它添加屬性和方法,如下所示。
1
2
3
4
5
6
7
var person = new Object();
person.name = "Nicholas";
person.age = 29;
person.job = "Software Engineer";
person.sayName = function(){
alert(this.name);
};
2、通過對象字面量創建對象
1
2
3
4
5
6
7
8
var person = {
name: "Nicholas",
age: 29,
job: "Software Engineer",
sayName: function(){
alert(this.name);
}
};
3、通過構造函數模式創建
1
2
3
4
5
6
7
8
9
10
function Person(name, age, job){
this.name = name;
this.age = age;
this.job = job;
this.sayName = function(){
alert(this.name);
};
}
var person1 = new Person("Nicholas", 29, "Software Engineer");
var person2 = new Person("Greg", 27, "Doctor");
4、通過原型模式創建
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function Person(){
}
Person.prototype = {
constructor: Person,
name : "Nicholas",
age : 29,
job : "Software Engineer",
friends : ["Shelby", "Court"],
sayName : function () {
alert(this.name);
}
};
var person1 = new Person();
var person2 = new Person();
person1.friends.push("Van");
alert(person1.friends); //"Shelby,Court,Van"
alert(person2.friends); //"Shelby,Court,Van"
alert(person1.friends === person2.friends); //true
5、組合使用構造函數模式和原型模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function Person(name, age, job){
this.name = name;
this.age = age;
this.job = job;
this.friends = ["Shelby", "Court"];
}
Person.prototype = {
constructor : Person,
sayName : function(){
alert(this.name);
}
}
var person1 = new Person("Nicholas", 29, "Software Engineer");
var person2 = new Person("Greg", 27, "Doctor");
person1.friends.push("Van");
alert(person1.friends); //"Shelby,Count,Van"
alert(person2.friends); //"Shelby,Count"
alert(person1.friends === person2.friends); //false
alert(person1.sayName === person2.sayName); //true
6、動態原型模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
function Person(name, age, job){
//屬性
this.name = name;
this.age = age;
this.job = job;
//方法
if (typeof this.sayName != "function"){
Person.prototype.sayName = function(){
alert(this.name);
};
}
}
var friend = new Person("Nicholas", 29, "Software Engineer");
friend.sayName();
7、寄生構造函數模式
1
2
3
4
5
6
7
8
9
10
11
12
function Person(name, age, job){
var o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.sayName = function(){
alert(this.name);
};
return o;
}
var friend = new Person("Nicholas", 29, "Software Engineer");
friend.sayName(); //"Nicholas"
8、穩妥構造函數模式
1
2
3
4
5
6
7
8
9
10
11
function Person(name, age, job){
//創建要返回的對象
var o = new Object();
//可以在這里定義私有變量和函數
//添加方法
o.sayName = function(){
alert(name);
};
//返回對象
return o;
}
本站僅提供存儲服務,所有內容均由用戶發布,如發現有害或侵權內容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
深入理解JavaScript面向對象的程序設計(一)——對象的創建
javascript 面向對象(對象的創建方式ES3)
簡單粗暴地理解js原型鏈
js對象定義的幾種方法
JS 創建對象(常見的幾種方法)
JavaScript中的對象,如何創建對象,創建對象的7種模式
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服

主站蜘蛛池模板: 安塞县| 财经| 呼伦贝尔市| 连平县| 泽库县| 拉萨市| 乌兰察布市| 江油市| 格尔木市| 满洲里市| 张家港市| 新乡市| 县级市| 嘉峪关市| 鸡泽县| 卢龙县| 山阴县| 阜新市| 四子王旗| 班玛县| 巫溪县| 白河县| 惠水县| 略阳县| 长顺县| 尤溪县| 治县。| 玉环县| 延寿县| 务川| 贡觉县| 桑植县| 霞浦县| 莎车县| 陇西县| 潼关县| 竹溪县| 绥棱县| 南木林县| 内乡县| 元氏县|