function Parent(name){
        this.name = name;
        this.arr = ['brother','sister','parents'];
    }
Parent.prototype.run = function () {
        return this.name;
    };
function Child(name,age){
        Parent.call(this,name);
        this.age =age;
    }
inheritPrototype(Parent,Child);//通过这里实现继承
var test = new Child('trigkit4',21);
    test.arr.push('nephew');
    alert(test.arr);//
    alert(test.run());//只共享了方法
var test2 = new Child('jack',22);
    alert(test2.arr);//引用问题解决
</script>
call和apply
全局函数apply和call可以用来改变函数中this的指向,如下:
复制代码 代码如下:
// 定义一个全局函数
    function foo() {
        console.log(this.fruit);
    }
// 定义一个全局变量
    var fruit = "apple";
    // 自定义一个对象
    var pack = {
        fruit: "orange"
    };
// 等价于window.foo();
    foo.apply(window);  // "apple",此时this等于window
    // 此时foo中的this === pack
    foo.apply(pack);    // "orange"
您可能感兴趣的文章:
