構(gòu)造函數(shù)

錯誤之處,歡迎指正。


1. 構(gòu)造函數(shù)的特性

  1. 通常使用大駝峰命名法來命名一個構(gòu)造函數(shù)(普通函數(shù)使用小駝峰命名法)。
  2. 使用new關(guān)鍵字來使用構(gòu)造函數(shù),此時構(gòu)造函數(shù)會默認(rèn)返回一個對象,并且此時構(gòu)造函數(shù)里面的this指向返回的這個對象。
function Person(name) {
    this.name = name;
}
const obj = new Person('chris');
console.log(obj);  
控制臺輸出結(jié)果
  1. 如果在構(gòu)造函數(shù)中添加return并且是一個原始值,那么不會有任何影響,構(gòu)造函數(shù)依然返回構(gòu)造的對象;如果是一個引用值,那么構(gòu)造函數(shù)會返回這個引用值。
  2. 如果使用new關(guān)鍵字使用構(gòu)造函數(shù),此時new.target是該函數(shù)體,如果不是使用new,new.targetundefined??梢杂脕頇z測是否是用new關(guān)鍵字使用構(gòu)造函數(shù)。
  3. 對象都是通過構(gòu)造函數(shù)構(gòu)造出來的。
const obj = {
    name: 'chris'
}

諸如我們平時的聲明一個對象的寫法,其實(shí)是一種語法糖,實(shí)際上:

const obj = new Object();
obj.name = 'chris';

實(shí)際上function也是通過構(gòu)造函數(shù)Function構(gòu)造出來的,函數(shù)字面量的寫法也屬于一種語法糖。那么實(shí)際上可以有這樣的關(guān)系:Function構(gòu)造一個構(gòu)造函數(shù),構(gòu)造函數(shù)產(chǎn)生對象。

  1. 為了增強(qiáng)原始類型的功能,javascript創(chuàng)建了Boolean 、Number、 String三個構(gòu)造函數(shù)。當(dāng)把一個原始類型當(dāng)對象使用時,javascript就會自動new 構(gòu)造函數(shù)()來調(diào)用屬性。
const id = 123.123;
console.log(id.toFixed(2));  //123.12 實(shí)際上:
console.log(new Number(id).toFixed(2));  //123.12  構(gòu)造函數(shù)返回對象,對象上面有屬性
  1. 實(shí)例/成員屬性,通過構(gòu)造函數(shù)創(chuàng)建的對象調(diào)用的屬性。
function Person() {
    this.name = 'chris';
}
const me = new Person();
console.log(me.name);  //chris 實(shí)例屬性

function的實(shí)例方法call

function test() {
    console.log(this.chris);
} 
test();  //this指向window 輸出undefined
const testObj = {
    chris: 'handsome'
}
test.call(testObj);  //handsome

call方法調(diào)用一個函數(shù)時,可以改變函數(shù)中this的指向,如上述例子中,用calltest函數(shù)中的this指向testObj。
callapply的區(qū)別就在于傳參方式不同。

function test(chris, age) {
    console.log(chris);
    console.log(age);
}
const testObj = {};
test.apply(testObj, ['handsome', 22]);  //handsome 22
test.call(testObj, 'handsome', 23);  //handsome 23

apply傳參時要放在數(shù)組里面。
bind用法:

function test(chris, age) {
    console.log(chris);
    console.log(age);
}
const testObj = {};
const newFunc = test.bind(testObj, 'handsome', 22); 
newFunc(); //handsome 22
newFunc(); //handsome 22

通常用call或者apply來將一個類數(shù)組轉(zhuǎn)換成數(shù)組:

function test() {
    console.log(arguments);  //類數(shù)組
    console.log(Array.isArray(arguments));  //false  用Array構(gòu)造函數(shù)的靜態(tài)方法判斷arguments是不是數(shù)組
    const newArr = Array.prototype.slice.call(arguments); //調(diào)用Array原型上的slice方法,并且此時this指向arguments,不傳參就代表不截取,返回完整數(shù)組。
    console.log(Array.isArray(newArr));  //true
    console.log(newArr); //[1,2,3,4,5]
}
test(1,2,3,4,5);
  1. 靜態(tài)屬性是指通過構(gòu)造函數(shù)調(diào)用的屬性。
function Person() {}
Person.type = 'human';
const me = new Person();
console.log(Person.type);  //human  靜態(tài)屬性
console.log(me.type);  //undefined

Object的靜態(tài)屬性:

function Person(name, age) {
    this.name = name;
    this.age = age
}
const me = new Person('chris', 22);
console.log(Object.keys(me));  //["name", "age"]
console.log(Object.values(me));  //["chris", 22]
console.log(Object.entries(me));  //[["name", "chris"], ["age", 22]]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容