最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript中new操作符的原理與實現(xiàn)詳解

 更新時間:2022年10月25日 15:55:59   作者:來碗鹽焗星球  
你知道new嗎?你知道new的實現(xiàn)原理嗎?你能手寫new方法嗎?不要擔心,這篇文件就來帶大家深入了解一下JavaScript中的new操作符,感興趣的小伙伴可以學習一下

一、new做了哪些事

先看看new的使用場景:

// 1、創(chuàng)建一個構(gòu)造函數(shù)
function Vehicle(name, price) {
    this.name = name
    this.price = price
}
?
// 2、new一個實例對象
let truck = new Vehicle()
console.log(truck); //Vehicle { name: undefined, price: undefined }
console.log(Object.prototype.toString.call(truck)); //[object Object]
?
// 傳入?yún)?shù)
let car = new Vehicle('car', '¥9999999')
console.log(car);
//Vehicle { name: 'car', price: '¥9999999' }

司空見慣的代碼,爛熟于心的寫法,那你知道new具體做了哪些事情嘛?從上述代碼可以看出,一個構(gòu)造函數(shù)使用new操作符調(diào)用的時候,會生成一個具有構(gòu)造函數(shù)相同屬性的新對象。是不是很奇怪?明明Vehicle是構(gòu)造函數(shù):

console.log(typeof Vehicle); //function

然而,經(jīng)過new的一番操作后,它的實例化是一個對象?。?!new到底做了哪些事情呢?對于這個例子,我們可以概括為以下事情:

?
    // 第一件:在構(gòu)造函數(shù)內(nèi)部,創(chuàng)建一個this對象
    let this = {
        name = name,
        price = price
    }
?
    // 第二件:返回this對象
    return this;
?
    // 第三件:給this對象的屬性賦值
    this.name = name
    this.price = price

很抽象,看不懂。。。進一步剖析如下:

function Person(name, gender) {
    console.log('賦值前的this=', this); //賦值前的this= Person {}
    this.name = name
    this.gender = gender
    console.log('賦值后的this=', this); //賦值后的this= Person { name: '小灰灰', gender: 'boy' }
}
?
let child = new Person('小灰灰', 'boy')//Person { name: '小灰灰', gender: 'boy' }
console.log(child);

由以上代碼可以看出,

第一:在構(gòu)造函數(shù)內(nèi)部有一個空的this對象,通過new操作符,會創(chuàng)建生成一個全新的對象(實例對象)。

第二:實例對象會執(zhí)行[[Prototype]]( .proto)鏈接,并且實例對象的this會指向構(gòu)造函數(shù)的this(實例對象會綁定函數(shù)調(diào)用的this)。通過new創(chuàng)建的實例對象最終被[[Prototype]]( .proto)鏈接到構(gòu)造函數(shù)的Prototype對象上。也就是說,實例對象的隱式原型===構(gòu)造函數(shù)的顯示原型

二、返回不同類型時有哪些表現(xiàn)

創(chuàng)建一個構(gòu)造函數(shù)X,通過new操作符,實例化X得到實例化對象x,打印x,一定會是X{...}這個對象嘛?當構(gòu)造函數(shù)內(nèi)部有返回值,并且返回的是不同類型的值,打印的結(jié)果又會是怎么樣呢?

function Student(id, name) {
    this.id = id
    this.name = name
?
    // 返回基本類型的值時:返回的結(jié)果依然是對象Student {name:xxx,age:xxx}
    // return null   //Student { id: '1001', name: 'cat' }
    // return undefined //Student { id: '1001', name: 'cat' }
    // return 123        //Student { id: '1001', name: 'cat' }
    // return 'hello world'   //Student { id: '1001', name: 'cat' }
    // return true  //Student { id: '1001', name: 'cat' }
    // return false  //Student { id: '1001', name: 'cat' }
    //return Symbol('abc')  //Student { id: '1001', name: 'cat' }
?
    // 返回引用類型時:
    //返回空對象時:返回的結(jié)果是空對象
    // return {}  //{}
    //返回函數(shù)時,返回的結(jié)果是函數(shù)
    return function() {} //[Function (anonymous)]
    // return [] //[]
    // return new Date() //2022-10-24T04:44:18.581Z
    // return new Error() //Error...
}
?
let student = new Student('1001', 'cat')
console.log(student); //構(gòu)造函數(shù)內(nèi)部返回不同類型的值時,這里的打印結(jié)果是一樣的嗎?

三、手寫new的實現(xiàn)原理

思路:new的實現(xiàn)原理核心是new做了哪些事情。

總結(jié):

(1)通過new操作符調(diào)用構(gòu)造函數(shù),會返回一個全新的對象,這個對象的屬性是構(gòu)造函數(shù)的參數(shù)。

若構(gòu)造函數(shù)內(nèi)部有返回值,且返回值是基本數(shù)據(jù)類型(number|string|null|undefined|Symbol|boolean),則實例對象的返回結(jié)果是原本的對象;

若返回值是引用數(shù)據(jù)類型(Object|Array|Function|Date|RegExp|Error),則實例對象的返回的結(jié)果就是引用類型對應的值。

(2)通過new操作符創(chuàng)建的實例對象的隱式原型會掛載到構(gòu)造函數(shù)的顯示原型上。實例對象.proto==構(gòu)造函數(shù).prototype。

(3)通過new操作符創(chuàng)建的實例對象的this會綁定調(diào)用函數(shù)的this 請看如下代碼:

// new的實現(xiàn)原理
function newPerson() {
    // 先return一個對象
    var obj = {};
    
    var constructor = Array.prototype.shift.call(arguments); //把數(shù)組的shift方法借給constructor使用
    
    // 實例對象的隱式原型===構(gòu)造函數(shù)的顯示原型
    obj._proto_ = constructor.prototype;
    var result = constructor.apply(obj, arguments);
    return typeof result === 'object' && result != 'null' ? result : obj;
}
?
let p = newPerson(Person, 'hunny')
console.log(p); //{ _proto_: {}, name: 'hunny', age: undefined }

以上就是JavaScript中new操作符的原理與實現(xiàn)詳解的詳細內(nèi)容,更多關(guān)于JavaScript new操作符的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

奇台县| 南宁市| 威宁| 松原市| 武夷山市| 尼木县| 维西| 苍梧县| 荣昌县| 洞口县| 商南县| 泾阳县| 阳东县| 南皮县| 日照市| 济宁市| 安丘市| 南和县| 盘山县| 巴中市| 区。| 信阳市| 乐山市| 涞源县| 新郑市| 宁晋县| 云浮市| 武乡县| 岳阳市| 兰坪| 盱眙县| 昆山市| 南昌县| 建瓯市| 临邑县| 固原市| 米泉市| 布尔津县| 化州市| 晴隆县| 万源市|