构造函数和实例化原理
生活随笔
收集整理的這篇文章主要介紹了
构造函数和实例化原理
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
(1)構(gòu)造函數(shù)內(nèi)部原理
- 1、在函數(shù)體最前面隱式的加上this = {};
- 2、執(zhí)行 this.xxx = xxx;
- 3、隱式的返回this
(2)實(shí)例化原理
new關(guān)鍵字實(shí)例化對(duì)象,改變this指向,由window到實(shí)例化出的對(duì)象本身
(3)代碼示例
function Car(color, brand) {// var this = {// color: color,// brand: brand// }this.color = color;this.brand = brand;// 隱式返回// return this;// 可以顯示返回引用值: {},[],function// var obj = {// color: 'red'// }// return obj;// 返回原始值無效// return '123'; } // 實(shí)例化構(gòu)造函數(shù): new 作用就是造出this var car1 = new Car('red', 'Benz'); console.log(car1.color);總結(jié)
以上是生活随笔為你收集整理的构造函数和实例化原理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何用python创建文件_如何使用Py
- 下一篇: android代码移除焦点,androi