面向?qū)ο?2-構(gòu)造函數(shù)注意事項

構(gòu)造函數(shù)注意事項

01 函數(shù)傳值

  • 函數(shù)傳值:可以將對象的方法作為參數(shù)傳遞

代碼示例

    //001 創(chuàng)建一個構(gòu)造函數(shù)
    function Person(name,age,toDoSomeThing) {

        //002 在構(gòu)造函數(shù)內(nèi)部設(shè)置對象的屬性和方法
        this.name = name;
        this.age = age;


        this.toDoSomeThing = toDoSomeThing;
    }

    //003 使用構(gòu)造函數(shù)創(chuàng)建對象
    var zhangsan = new Person("張三",18,function () {
        console.log("張三在讀書");
    });

    var lisi = new Person("李四",20,function () {
        console.log("李四在玩耍");
    });

02 類型判斷

  • 類型判斷:判斷某個對象是否由指定的構(gòu)造函數(shù)創(chuàng)建出來的
    • 利用 instanceOf----語法:對象 instanceOf 構(gòu)造函數(shù)
    • 注意點:所有的對象都是Objec類型

代碼示例

    function Person(name,age) {
        this.name = name;
        this.age = age;
    }

    function Dog(name,age) {
        this.name = name;
        this.age = age;
    }

    var obj1 = new Person("張三",20);
    var obj2 = new Dog("旺財",1);
    console.log(obj1);
    console.log(obj2);

    //判斷類型 判斷某個對象是否是由制定的構(gòu)造函數(shù)創(chuàng)建出來的
    //instanceOf 語法:對象 instanceOf 構(gòu)造函數(shù)
    //注意點:所有的對象都是Object類型

    console.log(obj1 instanceof Person);       //true
    console.log(obj2 instanceof Person);       //false
    console.log(obj1 instanceof Dog);          //false
    console.log(obj2 instanceof Dog);       //true

    console.log(obj1 instanceof Object);    //true
    console.log(obj2 instanceof Object);    //true

03 構(gòu)造器屬性

  • 屬性的名稱:constructor

  • 屬性的作用:指向創(chuàng)建該對象的構(gòu)造函數(shù),類似于現(xiàn)實生活中所有的產(chǎn)品都標有生產(chǎn)廠家一樣,獲取該對象的類型使用constructor屬性。

function Dog(name) {
        this.name = name;
        this.color = "黃色";
    }
console.log(dog.constructor);  //function Dog(name) {this.name = name;this.color = "黃色";}

04 函數(shù)調(diào)用

01 構(gòu)造函數(shù)可以像普通函數(shù)一樣不通過new關(guān)鍵字直接調(diào)用;如果函數(shù)沒有返回值,那么函數(shù)調(diào)用默認返回undefined

代碼示例

    //01 創(chuàng)建構(gòu)造函數(shù)
    function Person() {
        this.name = "張三";
        this.age = 20;
        this.sayName = function () {
            console.log(this.name);
        }
    }

    //02 使用構(gòu)造函數(shù)創(chuàng)建對象
    var p1 =Person();
    console.log(p1);  //undefined

02 在使用構(gòu)造函數(shù)創(chuàng)建對象的時候,如果沒有傳遞參數(shù),則()可以省略

代碼示例

       //01 創(chuàng)建構(gòu)造函數(shù)
    function Person() {
        this.name = "張三";
        this.age = 20;
        this.sayName = function () {
            console.log(this.name);
        }
    }

    //02 使用構(gòu)造函數(shù)創(chuàng)建對象
    var p1 = new Person();
    var p2 = new Person;    //說明:如果不需要傳遞參數(shù),則在調(diào)用構(gòu)造函數(shù)的時候()可以省略

05 this指向

01 如果使用new 構(gòu)造函數(shù)的方式調(diào)用,則this指向內(nèi)部默認創(chuàng)建出來的空對象

02 如果像調(diào)用普通函數(shù)一樣調(diào)用構(gòu)造函數(shù),則this指向全局對象window(不要這樣使用)

代碼示例

    //01 創(chuàng)建構(gòu)造函數(shù)
    function Person(name) {
        if(this==window){
            return new Person(name)
        }
//        if(!(this instanceof Person)){
//        
//            return new Person(name)
//        }
        this.name = name;
    }

    //02 使用構(gòu)造函數(shù)創(chuàng)建對象
    var p1 =Person('zhangsan');
    console.log(p1);  

以上代碼顯示,不論是否加new調(diào)用函數(shù),則均返回新創(chuàng)建的對象

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

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

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