關于new操作符的相關問題講解
來源:懂視網
責編:小OO
時間:2020-11-27 19:48:30
關于new操作符的相關問題講解
先看結果;function Person (name.age.job) { this.name = name this.age = age this.job = job}Person.prototype.sayName = function () { console.log(this.name)}// 使用new操作符var p1 = new Person(';laoyang';.';22';.';coding';)p1 instanceof Person // true// 不使用new 操作符var p2 = new Object()Person.call(p2.';xiaoyang';.';2';.'。比較不同;
導讀先看結果;function Person (name.age.job) { this.name = name this.age = age this.job = job}Person.prototype.sayName = function () { console.log(this.name)}// 使用new操作符var p1 = new Person(';laoyang';.';22';.';coding';)p1 instanceof Person // true// 不使用new 操作符var p2 = new Object()Person.call(p2.';xiaoyang';.';2';.'。比較不同;

以下就是new 操作符的一些問題,本篇將會解釋起相關問題。
先看結果
function Person (name, age, job) { this.name = name this.age = age this.job = job
}
Person.prototype.sayName = function () { console.log(this.name)
}// 使用new操作符var p1 = new Person('laoyang', '22', 'coding')
p1 instanceof Person // true// 不使用new 操作符var p2 = new Object()
Person.call(p2, 'xiaoyang', '2', 'test')
p2.__proto__ = Person.prototype
p2 instanceof Person // true
比較不同
// 使用new 操作符直接創建實例var p1 = new Person('laoyang', '22', 'coding')
// 不使用new 操作符var p2 = new Object()
// p2 創建成為一個對象 這時p2的原型是ObjectPerson.call(p2, 'xiaoyang', '2', 'test')
// Person構造函數在 p2 對象的環境內執行 這時p2已經是一個具有Person屬性的實例了,但原型是Objectp2.__proto__ = Person.prototype
// 最后把Person.prototype 賦值給p2.__proto__,讓p2的原型指向Person.prototype
不使用new 操作符創建實例的步驟:
Person.call(p2, 'xiaoyang', '2', 'test') // d
本篇展示了new操作符的相關問題,更多相關問題請關注Gxl網。
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
關于new操作符的相關問題講解
先看結果;function Person (name.age.job) { this.name = name this.age = age this.job = job}Person.prototype.sayName = function () { console.log(this.name)}// 使用new操作符var p1 = new Person(';laoyang';.';22';.';coding';)p1 instanceof Person // true// 不使用new 操作符var p2 = new Object()Person.call(p2.';xiaoyang';.';2';.'。比較不同;