溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

5種JS繼承方法

發布時間:2020-07-05 06:21:03 來源:網絡 閱讀:457 作者:pmlinjian 欄目:開發技術

<!DOCTYPE html>

<html>

<head>

<meta charset=" utf-8">

<meta name="author" content="http://www.jb51.net/" />

<title>5種JS繼承方法</title>


<script type="text/javascript">

//1、原型鏈繼承

function superType(){

this.property = true;

}

superType.prototype.getSuperValue = function(){

return this.property;

};

function subType(){

this.subProperty = false;

}

subType.prototype = new superType();

subType.prototype.getSubValue = function(){

return this.subProperty;

}; 

var intance = new subType();

alert(intance.getSubValue());


//2、借用構造函數繼承

function superType(){

this.colors = ["red", "blue", "gray"];

}

function subType(){

superType.call(this);

}

var intance1 = new subType();

intance1.colors.push("green");

alert(intance1.colors);


var intance2 = new subType();

alert(intance2.colors);


//3、組合繼承(原型鏈+借用構造函數)

function superType(){

this.name = name;

this.colors = ["red", "blak", "blue"];

}

superType.prototype.sayName = function(){

alert(this.name);

};

function subType(name, age){

superType.call(this, name);

this.age = age;

}

subType.prototype = new superType();

subType.prototype.sayAge = function(){

alert(this.age);

}

var intance1 = new subType("nicholas",23);

intance1.colors.push("green");

alert(intance1.colors);

intance1.sayName();

intance1.sayAge();

//4、寄生式繼承

function object(o){

            function F(){}

            F.prototype = o;

            return new F();

        }

function createAnother(original){

var clone = object(original);

clone.sayHi = function(){

alert("hi");

};

return clone;

}

var person = {

name : "nicholas",

friends : ["Sheby", "Court", "van"]

};

var anotherPerson = createAnother(person);

anotherPerson.sayHi(); // hi


//5、寄生組合繼承

 function object(o){

            function F(){}

            F.prototype = o;

            return new F();

        }


 function inheritPrototype(subType, superType){

            var prototype = object(superType.prototype);   //create object

            prototype.constructor = subType;               //augment object

            subType.prototype = prototype;                 //assign object

        }

                                

        function SuperType(name){

            this.name = name;

            this.colors = ["red", "blue", "green"];

        }

        

        SuperType.prototype.sayName = function(){

            alert(this.name);

        };


        function SubType(name, age){  

            SuperType.call(this, name);

            

            this.age = age;

        }


        inheritPrototype(SubType, SuperType);

        

        SubType.prototype.sayAge = function(){

            alert(this.age);

        };

        

        var instance1 = new SubType("Nicholas", 29);

        instance1.colors.push("black");

        alert(instance1.colors);  //"red,blue,green,black"

        instance1.sayName();      //"Nicholas";

        instance1.sayAge();       //29

        

       

        var instance2 = new SubType("Greg", 27);

        alert(instance2.colors);  //"red,blue,green"

        instance2.sayName();      //"Greg";

        instance2.sayAge();       //27

</script>

</head>

<body>


</body>

</html>


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女