构造函数和实例化原理
生活随笔
收集整理的這篇文章主要介紹了
构造函数和实例化原理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
(1)構造函數內部原理
- 1、在函數體最前面隱式的加上this = {};
- 2、執行 this.xxx = xxx;
- 3、隱式的返回this
(2)實例化原理
new關鍵字實例化對象,改變this指向,由window到實例化出的對象本身
(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'; } // 實例化構造函數: new 作用就是造出this var car1 = new Car('red', 'Benz'); console.log(car1.color);總結
以上是生活随笔為你收集整理的构造函数和实例化原理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何用python创建文件_如何使用Py
- 下一篇: android代码移除焦点,androi