Javascript继承(一)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>类式继承</title>
</head>
<body>

</body>
</html>
<script>
 //    类式继承的方式,用call和apply的方式将this指针传给父函数

    function Super(){                       //不能起super,特殊字符
        this.colors = ["red","blue","orange"];
        this.names = ["红色","蓝色","橙色"]
    }
    function Sub() {
        Super.call(this);
    }

    var a = new Sub();
    //给a.colors添加颜色 white
    a.colors.push("white");
    console.log(a.colors);   //["red","blue","orange","white"]
    console.log(a.names);    //["红色","蓝色","橙色"]
    //给a.names添加两个颜色名:白色,粉色
    a.names.push("白色","粉色");
    console.log(a.names);     //["红色","蓝色","橙色","白色","粉色"]
    var b = new Sub();
    console.log(b);
    //array   colors:["red","blue","orange"]             names:["红色","蓝色","橙色"]

</script>

猜你喜欢

转载自blog.csdn.net/qq_39793127/article/details/79012428