Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="借用构造函数">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
    function SuperType(name){
        this.color = ["yellow","green"];
        this.name  = name;
        this.getName = function(){
            return this.name;
        };
    }
    function SubType(name){
        SuperType.call(this,name);
    }
    var instance  = new SubType("xiaodong");
    instance.color.push("black");
    var instance2 = new SubType();
    console.log(instance.color);//["yellow", "green", "black"]
    console.log(instance.name);//xiaodong
    console.log(instance2.color);// ["yellow", "green"]
    console.log(instance.getName == instance2.getName);//false 方法没有达到共用
    //借用构造函数解决了属性值共享的问题,以及实例向超级类传递参数的问题. 缺点 方法也是在函数中定义 无法解决函数共用的问题
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
buxukupro
0viewers