摘要:所以,可以創(chuàng)建自定義的構(gòu)造函數(shù),從而定義自定義對(duì)象類型的屬性和方法。如對(duì)于構(gòu)造函數(shù)原型屬性以及實(shí)例之間的關(guān)系,參見(jiàn)高級(jí)程序設(shè)計(jì)一書中第章節(jié)。穩(wěn)妥構(gòu)造函數(shù)模式穩(wěn)妥對(duì)象,指的是沒(méi)有公共屬性,且其方法也不引用的對(duì)象如
創(chuàng)建對(duì)象
Object 構(gòu)造函數(shù)或?qū)ο笞置媪慷伎梢杂脕?lái)創(chuàng)建單個(gè)對(duì)象。但這個(gè)方法的缺點(diǎn)非常明顯:同一個(gè)接口創(chuàng)建很可耐多對(duì)象會(huì)產(chǎn)生大量的重復(fù)代碼。為了解決這個(gè)問(wèn)題,人們開(kāi)始使用工廠模式的一種變體。
工廠模式(摒棄,不推薦)這個(gè)模式?jīng)]有解決對(duì)象識(shí)別的問(wèn)題(即怎樣知道一個(gè)對(duì)象的類型)。如:
具體的創(chuàng)建單個(gè)對(duì)象:
var person = {}; person.name = "Oliver"; person.age = 18; person.sayName = function(){ return this.Name; };
改變成工廠模式:
function createPerson(name,age){ var obj = {}; obj.name = name; obj.age = age; obj.sayName = function(){ return this.name }; return obj; //注意這里要返回obj 對(duì)象,這樣才能把obj 對(duì)象傳給createPerson 變量。 } var newPerson = createPerson("Oliver",18);構(gòu)造函數(shù)模式
構(gòu)造函數(shù)可以創(chuàng)建特定類型的對(duì)象。所以,可以創(chuàng)建自定義的構(gòu)造函數(shù),從而定義自定義對(duì)象類型的屬性和方法。如:
function Person(name,age){ //注意大小寫,構(gòu)造函數(shù)應(yīng)該把第一個(gè)字幕大寫化 this.name = name; this.age = age; this.sayName = function (){ return this.name; }; } var newPerson = new Person("Oliver",18); document.write(newPerson.name); //Oliver document.write(newPerson.age); //18 document.write(newPerson.sayName()); //Oliver
確實(shí)相當(dāng)方便
這里一定要記住,構(gòu)造函數(shù)都應(yīng)該以一個(gè)大寫字母開(kāi)頭,用來(lái)區(qū)分其他的函數(shù)
又如:
function Person(name,age){ //注意大小寫,構(gòu)造函數(shù)應(yīng)該把第一個(gè)字幕大寫化 this.name = name; this.age = age; this.sayName = function (){ return this.name; }; } var person1 = new Person("Oliver",18); var person2 = new Person("Troy",24); document.write(person1.constructor == Person); //true document.write(person2.constructor == Person); //true
這里的person1 和person2 分別保存著Person 的一個(gè)不同的實(shí)例。兩個(gè)對(duì)象都有一個(gè)constructor(構(gòu)造函數(shù))屬性,該屬性指向Person。
在上面這個(gè)例子中,person1 和person2 即是Object 的實(shí)例,同時(shí)也是Person 的實(shí)例??梢酝ㄟ^(guò)instanceof 操作符來(lái)驗(yàn)證:
console.log((person1 instanceof Object) && (person2 instanceof Person)); //true
以這種方式創(chuàng)建構(gòu)造函數(shù)是定義在Global 中的(window 對(duì)象)
將構(gòu)造函數(shù)當(dāng)做函數(shù)任何函數(shù),只要通過(guò)new 操作符來(lái)調(diào)用,那它就可以座位構(gòu)造函數(shù);而任何函數(shù),如果不通過(guò)new 操作符來(lái)調(diào)用,那它就跟普通函數(shù)沒(méi)區(qū)別。如下面這個(gè)構(gòu)造函數(shù):
function Car(name,color,sound){ this.name = name; this.color = color; this.sound = function(){ return sound; }; console.log(this.name + " " + this.color + " " + this.sound()); }
如果當(dāng)做構(gòu)造函數(shù)來(lái)使用:
var benz = new Car("C200","White","Boom Boom"); //C200 White Boom Boom
如果作為普通函數(shù)來(lái)調(diào)用:
Car("Benz","White","Boom!"); //Benz White Boom! console.log(window.name + window.color + window.sound()); //BenzWhiteBoom!
如果在另一個(gè)對(duì)象的作用域中調(diào)用:
var cars = {}; Car.call(cars,"Benz","White","Boom Boom!"); document.write(cars.sound()); //Boom Boom!構(gòu)造函數(shù)的問(wèn)題
問(wèn)題是每個(gè)方法都要在每個(gè)實(shí)例是重新創(chuàng)建一遍??捎猛ㄟ^(guò)把內(nèi)部的函數(shù)轉(zhuǎn)移到外部來(lái)解決這些問(wèn)題。如:
function Car(name,color){ this.name = name; this.color = color; this.show = show; } function show(){ console.log(this.name + this.color); } var benz = new Car("Benz","white"); benz.show(); //Benzwhite
但這個(gè)問(wèn)題是完全沒(méi)有了封裝性可言。不過(guò)可以通過(guò)原型模式來(lái)解決。
原型模式function Person(){}; Person.prototype.name = "Oliver"; Person.prototype.age = 18; Person.prototype.sayName = function(){ console.log(this.name); }; var person1 = new Person(); person1.sayName(); //Oliver var person2 = new Person(); person2.sayName(); //Oliver; console.log(person1.sayName == person2.sayName); //true
與構(gòu)造函數(shù)不同的是,新對(duì)象的這些屬性和方法是由所有實(shí)例共享的。這里兩個(gè)新的person 訪問(wèn)的都是同一組屬性和同一個(gè)sayName() 函數(shù)。
理解原型對(duì)象以上面的Person 為例,Person 構(gòu)造函數(shù)里面存在一個(gè)prototype 屬性,這個(gè)屬性指向原型對(duì)象Person Prototype,該P(yáng)erson Prototype 里面包含了constructor 屬性,該屬性又指向構(gòu)造函數(shù)Person。構(gòu)造函數(shù)的實(shí)例包含了一個(gè)[[Prototype]]的內(nèi)部屬性,該內(nèi)部屬性則指向Person Prototype。如:
function Person(){}; Person.prototype.name = "Oliver"; Person.prototype.age = 18; Person.prototype.sayName = function(){ console.log(this.name); }; var person1 = new Person(); person1.sayName(); //Oliver var person2 = new Person(); person2.sayName(); //Oliver; console.log(Person.prototype); /* age: 18 constructor: function Person() {} name: "Oliver" sayName: function () { __proto__: Object */ console.log(Person.prototype.constructor); //function Person() {} console.log(Object.getPrototypeOf(person1)); /* age: 18 constructor: function Person() {} name: "Oliver" sayName: function () { __proto__: Object */
對(duì)于構(gòu)造函數(shù)、原型屬性以及實(shí)例之間的關(guān)系,參見(jiàn)《js高級(jí)程序設(shè)計(jì)》一書中第6.2.3 章節(jié)。
兩個(gè)方法:isPrototypeOf()和Object.getProtytypeOf()(ECMAScript 5)。前者是用來(lái)確定[[Prototype]];后者是用來(lái)返回[[Prototype]]值。如:
console.log(Person.prototype.isPrototypeOf(person1)); //true console.log(Object.getPrototypeOf(person1).name); //Oliver console.log(Object.getPrototypeOf(person1)); /* age: 18 constructor: function Person() {} name: "Oliver" sayName: function () { __proto__: Object */
為對(duì)象添加一個(gè)屬性時(shí),這個(gè)屬性會(huì)屏蔽原型對(duì)象中的同名屬性,但是并不會(huì)修改那個(gè)屬性。如果使用delete 刪除這個(gè)屬性,就可以重新訪問(wèn)原型中的屬性。如:
function Person(){}; Person.prototype.name = "Oliver"; Person.prototype.age = 18; Person.prototype.sayName = function(){ console.log(this.name); }; var person1 = new Person(); person1.sayName(); //Oliver 原型中的Name person1.name = "Troy"; person1.sayName(); //Troy 實(shí)例中的Name delete person1.name; person1.sayName(); //Oliver 原型中的Name
每次讀取某個(gè)對(duì)象的某個(gè)屬性,都會(huì)從實(shí)例本身開(kāi)始搜索,如果沒(méi)有找到給定名字的屬性,則會(huì)在原型對(duì)象中再次搜索。
方法hasOwnProperty()檢測(cè)屬性如果在對(duì)象實(shí)例中時(shí),返回true。如:
console.log(person1.hasOwnProperty("age")); //false age屬性來(lái)自于原型 console.log(person1.hasOwnProperty("name")); //true name屬性來(lái)自于實(shí)例原型與in 操作符
兩種方法使用in 操作符:多帶帶使用和for-in 循環(huán)中使用。
多帶帶使用時(shí),in 返回true 說(shuō)明該屬性存在于實(shí)例或原型中。如:
function Person(){}; Person.prototype.name = "Oliver"; Person.prototype.age = 18; Person.prototype.sayName = function(){ console.log(this.name); }; var person1 = new Person(); person1.name = "Troy"; person1.sayName(); //Troy 實(shí)例中的Name console.log("name" in person1); //true name屬性在實(shí)例或原型中 console.log(person1.hasOwnProperty("name")); //true name屬性在實(shí)例中 //上面兩個(gè)就說(shuō)明name屬性一定在實(shí)例中
又如:
function Person(){}; Person.prototype.name = "Oliver"; Person.prototype.age = 18; Person.prototype.sayName = function(){ console.log(this.name); }; var person1 = new Person(); person1.name = "Troy"; person1.sayName(); //Troy 實(shí)例中的Name var person2 = new Person(); console.log("name" in person1); //true name屬性在實(shí)例或原型中 console.log(person1.hasOwnProperty("name")); //true name屬性在實(shí)例中 //上面兩個(gè)就說(shuō)明name屬性一定在實(shí)例中 console.log("name" in person2); //true console.log(person2.hasOwnProperty("name")); //false //上面兩個(gè)說(shuō)明name屬性一定在原型中
自定義一個(gè)函數(shù)hasPrototypeProperty(object,name);即同時(shí)使用上面兩個(gè)方法來(lái)確定屬性到底是不是存在于實(shí)例中。如:
function Person(){}; Person.prototype.name = "Oliver"; Person.prototype.age = 18; Person.prototype.sayName = function(){ console.log(this.name); }; var person1 = new Person(); person1.name = "Troy"; person1.sayName(); //Troy 實(shí)例中的Name var person2 = new Person(); function hasPrototypeProperty(object,name){ console.log((name in object) && !(object.hasOwnProperty(name))) } hasPrototypeProperty(person2,"name"); //true name屬性是在原型中 hasPrototypeProperty(person1,"name"); //false name屬性是在實(shí)例中
用Object.defineProperty()方法定義的屬性:
function Person(){}; Person.prototype.name = "Oliver"; Person.prototype.sayName = function(){ console.log(this.name); }; var person1 = new Person(); Object.defineProperty(person1, "age", { value: 18 }) console.log(person1.hasOwnProperty("age")); //true age屬性是實(shí)例屬性
關(guān)于for-in、[[enumerable]]、defineProperty、hasOwnProperty 的例子:
var person1 = { age: 18 }; Object.defineProperty(person1, "name", { value: "Oliver", enumerable: true }) for(var x in person1){ console.log(x); } console.log(person1.hasOwnProperty("name")); //true
又如:
function Person(age){ this.age = age; } var person1 = new Person(18); Object.defineProperty(person1, "name", { value: "Oliver", enumerable: false }) for(var x in person1){ console.log(x); //用defineProperty 定義的name 屬性是實(shí)例屬性,這里不會(huì)枚舉到 } console.log(person1.hasOwnProperty("name")); //true
又如:
function Person(){}; Person.prototype.age = 18; var person1 = new Person(); Object.defineProperty(person1, "name", { value: "Oliver", enumerable: false }) for(x in person1){ console.log(x); //這里仍然不回枚舉到自定義的name 實(shí)例屬性 }
但是:
function Person(){}; Person.prototype.age = 18; Person.prototype.name = "Oliver"; var person1 = new Person(); Object.defineProperty(person1, "name", { enumerable: false }) for(x in person1){ console.log(x); //這里則返回枚舉到自定義的name 原型屬性 }
原型屬性的[[enumerable]]設(shè)置為false,調(diào)用for-in 仍然可以被枚舉到。
另外,Object.keys()方法可以返回所有可枚舉屬性的字符串?dāng)?shù)組:
function Person(){}; Person.prototype.age = 18; Person.prototype.name = "Oliver"; var person1 = new Person(); Object.defineProperty(person1, "sound", { value: "miao~", enumerable: true //可枚舉 }); Object.defineProperty(person1, "sound2", { value: "wang~", enumerable: false //不可枚舉 }); console.log(Object.keys(Person.prototype)); //["age", "name"] console.log(Object.keys(person1)); //["sound"]
Object.getOwnPropertyName()方法,則可以返回?zé)o論可否枚舉的所有實(shí)例屬性:
function Person(){}; Person.prototype.age = 18; Person.prototype.name = "Oliver"; var person1 = new Person(); Object.defineProperty(person1, "sound", { value: "miao~", enumerable: true //可枚舉 }); Object.defineProperty(person1, "sound2", { value: "wang~", enumerable: false //不可枚舉 }); console.log(Object.keys(Person.prototype)); //["age", "name"] console.log(Object.keys(person1)); //["sound"] console.log(Object.getOwnPropertyNames(Person.prototype)); //["constructor", "age", "name"] console.log(Object.getOwnPropertyNames(person1)); //["sound","sound2"]更簡(jiǎn)單的原型語(yǔ)法
即字面量方法:
function Person(){}; Person.prototype = { name: "Oliver", age: 18, sayName: function(){ console.log(this.name); } }; var person1 = new Person(); console.log(Person.prototype.constructor); //不再指向Person()構(gòu)造函數(shù) function People(){}; People.prototype.name = "Troy"; People.prototype.age = 26; People.prototype.sayName = function(){ console.log(this.name); }; var people1 = new People(); console.log(People.prototype.constructor); //這里則指向People()構(gòu)造函數(shù)
上面第一種就是字面量方法。但是由此帶來(lái)的問(wèn)題是,他的原型對(duì)象中的constructor 屬性將不再指向上個(gè)例子中的Person() 構(gòu)造函數(shù)了。(其實(shí)我們本質(zhì)上是重寫了prototype對(duì)象)
如果constructor 值真的非常重要,則只需要把它設(shè)置回適當(dāng)?shù)闹稻涂梢粤耍?/p>
function Person(){}; Person.prototype = { constructor: Person, name: "Oliver", age: 18, sayName: function(){ console.log(this.name); } }; var person1 = new Person(); console.log(Person.prototype.constructor); //重新指向Person()構(gòu)造函數(shù) function People(){}; People.prototype.name = "Troy"; People.prototype.age = 26; People.prototype.sayName = function(){ console.log(this.name); }; var people1 = new People(); console.log(People.prototype.constructor); //這里則指向People()構(gòu)造函數(shù)
然而用字面量的方法導(dǎo)致的問(wèn)題仍然沒(méi)有結(jié)束,以上面這種方式重設(shè)constructor 屬性會(huì)導(dǎo)致[[Enumerable]]特性被設(shè)置為true。因此在支持ECMAScript 5 的js 引擎中可以用Object.defineProperty()方法把它修改為false:
function Person(){}; Person.prototype = { constructor: Person, name: "Oliver", age: 18, sayName: function(){ console.log(this.name); } }; var person1 = new Person(); console.log(Person.prototype.constructor); for (var x in person1){ console.log(x); //這里會(huì)出現(xiàn)constructor,但是我們實(shí)際上不應(yīng)該讓他能夠被枚舉出 } Object.defineProperty(Person.prototype, "constructor", { enumerable: false }); for (var x in person1){ console.log(x); //這里就不會(huì)出現(xiàn)constructor 了,但是這種方法只支持ECMAScript 5的js 引擎 } /* [Log] function Person() {} (repetition.html, line 130) [Log] constructor (repetition.html, line 132) [Log] name (repetition.html, line 132) [Log] age (repetition.html, line 132) [Log] sayName (repetition.html, line 132) [Log] name (repetition.html, line 140) [Log] age (repetition.html, line 140) [Log] sayName (repetition.html, line 140) */原型的動(dòng)態(tài)性
我們對(duì)原型對(duì)象所做的任何修改都能立即從實(shí)例上反應(yīng)出來(lái)。因?yàn)閷?shí)例與原型之間的鏈接只不過(guò)是一個(gè)指針而不是副本:
function Person(){}; var person = new Person(); //person在Person()構(gòu)造函數(shù)修改之前創(chuàng)建的 Person.prototype.name = "Oliver"; console.log(person.name); //仍然會(huì)出現(xiàn)實(shí)時(shí)的變化
但是如果重寫了prototype 則就不同了,因?yàn)閷?shí)例的[[Prototype]]會(huì)指向原型對(duì)象,如果修改了原來(lái)的原型對(duì)象,則就是切斷了構(gòu)造函數(shù)與最初原型之間的聯(lián)系:
function Person(){}; var person = new Person(); Person.prototype = { //這里重寫了Person.prototype,屬于新的Person.prototype constructor: Person, name: "Oliver" } console.log(person.name); //原型對(duì)象被修改了,指針仍然指向舊的Person.prototype
從這里就可以很明顯看出,Person.prototype={},實(shí)際上字面量方法就是重寫了原型對(duì)象。如果是Person.prototype.name="Oliver",則并不是重寫而是修改,不會(huì)創(chuàng)建“新的原型對(duì)象?!?/p>
原生對(duì)象的原型《js高級(jí)程序設(shè)計(jì)》一書中6.2.3 中的圖6-3 很清楚的描述了該原理
所有原生的引用類型(Object、Array、String等等)都在其構(gòu)造函數(shù)的原型上定義了方法。同時(shí),我們也可以給原生對(duì)象自定義方法:
var array = new Array(); Array.prototype.name = function(){ console.log("Array") }; array.push("hello ","there"); console.log(array); array.name();
也可以修改:
var array = new Array(); Array.prototype.toString = function(){ return("Array") }; array.push("hello ","there"); console.log(array.toString()); //這樣就抹去了toString()方法
強(qiáng)烈不推薦修改和重寫原生對(duì)象的原型
原型對(duì)象的問(wèn)題就是包含引用類型值的屬性來(lái)說(shuō),問(wèn)題比較嚴(yán)重。具體體現(xiàn)在原型中的屬性被實(shí)例共享:
function Person(){}; Person.prototype = { constructor: Person, name: "Oliver", age: 18, friends: ["Troy","Alice"] } var person1 = new Person(); var person2 = new Person(); person1.friends.push("Ellen"); console.log(person1.friends); //["Troy", "Alice", "Ellen"] console.log(person2.friends); //["Troy", "Alice", "Ellen"] //兩者完全相同,因?yàn)樵椭械脑搶傩员粚?shí)例所共享,push()方法只是修改了person1.friends,沒(méi)有重寫 person1.friends = ["Troy", "Alice"]; console.log(person1.friends); //["Troy", "Alice"] console.log(person2.friends); //["Troy", "Alice", "Ellen"] //雖然可以通過(guò)重寫和覆蓋來(lái)解決該問(wèn)題,但是仍然非常麻煩 console.log(person1.hasOwnProperty("friends")); //true; console.log(person2.hasOwnProperty("friends")); //false; //這里就可以看到,重寫能解決問(wèn)題是因?yàn)橹貙憣?dǎo)致它創(chuàng)建了實(shí)例屬性"friends"
這里可以看出,如果我們的初衷像這樣只是想創(chuàng)建一個(gè)共享的數(shù)組,那么當(dāng)然不會(huì)有什么問(wèn)題;但是實(shí)例一般都會(huì)有自己的屬性,所以不應(yīng)該多帶帶使用原型模式。而是組合使用構(gòu)造函數(shù)模式和原型模式。
組合使用構(gòu)造函數(shù)模式和原型模式這是一種用來(lái)定義引用類型的一種默認(rèn)模式:
function Person(name,age){ this.name = name; this.age = age; this.friends = []; } //獨(dú)享的部分 Person.prototype = { constructor: Person, sayName: function(){ return this.name; } } //共享的部分 var person1 = new Person("Oliver",18); var person2 = new Person("Troy",24); person1.friends.push("Alice","Mark"); person2.friends.push("Mac"); console.log(person1.friends.toString()); console.log(person2.friends.toString()); /* [Log] Alice,Mark (repetition.html, line 228) [Log] Mac (repetition.html, line 229) */動(dòng)態(tài)原型模式
可以通過(guò)檢查某個(gè)應(yīng)該存在的方法是否有效,來(lái)決定是否需要初始化原型:
function Person(name,age){ this.name = name; this.age = age; if (typeof this.sayName != "function"){ Person.prototype.sayName = function(){ return (this.name); }; } } var person = new Person("Oliver",18); console.log(person.sayName()); //Oliver
實(shí)際上就是把下面代碼封裝在了構(gòu)造函數(shù)中:
function Person(name,age){ this.name = name; this.age = age; } Person.prototype.sayName = function(){ return(this.name); }; var person = new Person("Troy",24); console.log(person.sayName()); //Troy寄生構(gòu)造函數(shù)模式
世紀(jì)撒好難過(guò)跟工廠模式一樣。建議在可以使用其他模式的情況下,不要使用該模式。
穩(wěn)妥構(gòu)造函數(shù)模式穩(wěn)妥對(duì)象,指的是沒(méi)有公共屬性,且其方法也不引用this 的對(duì)象如:
function Person(name,age){ var obj = new Object(); obj.sayName = function(){ console.log(name); }; return obj; } var person1 = Person("Oliver",18); person1.sayName(); //Oliver
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/86248.html
摘要:如繼承了這里就不必寫該方法的主要優(yōu)勢(shì)就是可以在子類型構(gòu)造函數(shù)中向超類型構(gòu)造函數(shù)傳遞參數(shù)。以上原型式繼承通常只是想讓一個(gè)對(duì)象與另一個(gè)對(duì)象保持類似的情況下,原型式繼承是完全可以勝任的。 繼承 繼承分為接口繼承和實(shí)現(xiàn)繼承;接口繼承只繼承方法簽名,實(shí)現(xiàn)繼承則繼承實(shí)際的方法;由于函數(shù)沒(méi)有簽名,所以ECMAScript 中沒(méi)有接口繼承,只能依靠原型鏈來(lái)實(shí)現(xiàn)實(shí)現(xiàn)繼承。 原型鏈 基本思想是利用原型鏈讓...
摘要:描述符對(duì)象就是上面提到的個(gè)描述其行為的特性和。真是奇怪讀取屬性的特征使用的方法,可以取得給定屬性的描述符。接收兩個(gè)參數(shù)所在的對(duì)象和要讀取其描述符的屬性名稱。 對(duì)象的基本概念 面向?qū)ο螅∣bject-Oriented,OO),的語(yǔ)言最大的特征就是它們都有類的概念,通過(guò)類可以創(chuàng)建任意多個(gè)具有相同屬性和方法的對(duì)象。 創(chuàng)建自定義對(duì)象最簡(jiǎn)單的方式就是創(chuàng)建一個(gè)Object 的實(shí)例,然后再給他添加屬...
摘要:當(dāng)談到語(yǔ)言與其他編程語(yǔ)言相比時(shí),你可能會(huì)聽(tīng)到一些令人困惑東西,其中之一是工廠函數(shù)和構(gòu)造函數(shù)。好的,讓我們用構(gòu)造函數(shù)做同樣的實(shí)驗(yàn)。當(dāng)我們使用工廠函數(shù)創(chuàng)建對(duì)象時(shí),它的指向,而當(dāng)從構(gòu)造函數(shù)創(chuàng)建對(duì)象時(shí),它指向它的構(gòu)造函數(shù)原型對(duì)象。 showImg(https://segmentfault.com/img/bVbr58T?w=1600&h=900); 當(dāng)談到JavaScript語(yǔ)言與其他編程語(yǔ)言...
摘要:被覆蓋級(jí)事件處理事件名,事件處理函數(shù),事件捕獲事件冒泡清除事件處理要使用級(jí)事件處理程序不會(huì)被覆蓋而是會(huì)一步一步的解析執(zhí)行。 一,變量1.可以用new Array(1,2);來(lái)定義數(shù)組。2.可以通過(guò)為變量賦值為null來(lái)清除變量,如: //首先定義一個(gè)變量 var i1=10; i1=null; //此時(shí)的i1就被清除了 在函數(shù)里面這樣定義變量的時(shí)候要注意 funtion demo()...
摘要:構(gòu)造函數(shù)的兩個(gè)特征函數(shù)內(nèi)部使用了,指向所要生成的對(duì)象實(shí)例。將一個(gè)空對(duì)象的指向構(gòu)造函數(shù)的屬性,這個(gè)對(duì)象就是要返回的實(shí)例對(duì)象。用面向?qū)ο箝_(kāi)發(fā)時(shí),把要生成的實(shí)例對(duì)象的特有屬性放到構(gòu)造函數(shù)內(nèi),把共有的方法放到構(gòu)造函數(shù)的里面。 JS中面向?qū)ο蟮母拍?面向?qū)ο驩OP是一種組織代碼結(jié)構(gòu)、實(shí)現(xiàn)功能過(guò)程的思維方式。它將真實(shí)世界各種復(fù)雜的關(guān)系,抽象為一個(gè)個(gè)對(duì)象,然后由對(duì)象之間的分工與合作,完成對(duì)真實(shí)世界的...
閱讀 4302·2021-11-17 09:33
閱讀 3420·2021-10-08 10:05
閱讀 3350·2021-09-22 15:36
閱讀 1307·2021-09-06 15:02
閱讀 2912·2019-08-29 12:45
閱讀 1724·2019-08-26 13:40
閱讀 3583·2019-08-26 13:37
閱讀 568·2019-08-26 13:37