原型链与call/apply

 Person.prototype.name = "sunney"
    function Person() {

    }
    var person = new Person();
    console.log(person.name); //"sunney

    Person.prototype.name = "cherry";
    console.log(person.name); //"cherry

    Person.prototype = {
        name:"xiaowang"
    }
    console.log(person.name) //sunney
    等同于
    var obj = {name:"a"};
    var obj1 = obj;
    obj = {name:"b"}
    console.log(obj1) //{name: "a"}
    console.log(obj) //{name: "b"}

    Person.prototype.name = "sunney"
    function Person() {

    }
    Person.prototype = {
            name:"xiaowang"
        }
    var person = new Person();
    console.log(person.name); //xiaowang

    /**
     * this指向
     * 谁调用就会指向谁
     */
    Person.prototype = {
        name:"a",
        sayName:function () {
            console.log(this.name)
        }
    }
    function Person() {
        this.name = "b"
    }
    var person = new Person();
    console.log(person.sayName()) //b
    console.log(Person.prototype.sayName()) //a
    /**
     * call改变this指向
     * 第一个参数代表指向,第二个开始代表实参
     */
    function Person(name,age) {
        this.name = name;
        this.age =age
    }
    var person = new Person("deng",300);
    var obj = {};
    Person.call(obj,"cheng",100);
    console.log(obj); //{name: "cheng", age: 100}
    console.log(person);//Person {name: "deng", age: 300}
    /**
     * 工厂
     */
    function Wheel(wheelSize,style) {
        this.style = style;
        this.wheelSize = wheelSize;
    }
    function  Sit(c,sitColor) {
        this.c = c;
        this.sitColor = sitColor;
    }
    function Model(height,width,len) {
        this.height = height;
        this.width = width;
        this.len = len;
    }
    function Car(wheelSize,style,c,sitColor,height,width,len) {
        Wheel.call(this,wheelSize,style)
        Sit.call(this,c,sitColor)
        Model.call(this,height,width,len)
    }
    var car = new Car(100,"花里胡哨","真皮","黄色",1900,4900,2000) //Car {style: "花里胡哨", wheelSize: 100, c: "真皮", sitColor: "黄色", height: 1900,4900,2000}
    /**
     * call 需要把实参按照形参列表的个数传进去(一一对应)
     * apply 需要传一个arguments//一定传数组
     */
    function Wheel(wheelSize,style) {
        this.style = style;
        this.wheelSize = wheelSize;
    }
    function  Sit(c,sitColor) {
        this.c = c;
        this.sitColor = sitColor;
    }
    function Model(height,width,len) {
        this.height = height;
        this.width = width;
        this.len = len;
    }
    function Car(wheelSize,style,c,sitColor,height,width,len) {
        Wheel.apply(this,[wheelSize,style])
        Sit.apply(this,[c,sitColor])
        Model.apply(this,[height,width,len])
    }
    var car = new Car(100,"花里胡哨","真皮","黄色",1900,4900,2000) //Car {style: "花里胡哨", wheelSize: 100, c: "真皮", sitColor: "黄色", height: 1900,4900,2000}
    /**
     * 继承同一个原型
     */
    Father.prototype.lastName = "Deng";
    function Father() {

    }
    function Son() {

    }
    Son.prototype = Father.prototype;
    var son = new Son();
    var father = new Father();
    //都继承了  Father.prototype.lastName
    /**
     * 继承最终写法
     */
     function inner(B,A) {
        function F(){};
        F.prototype = A.prototype;
        B.prototype = new F;
        B.prototype.constructor = B; //把constructor指向改成继承者自身
        B.prototype.final = A.prototype; //继承自谁 保存下来 以后可能有用
    }
    Father.prototype.name = "李";
     function Father() {}
     function Son() {}
     inner(Son,Father);
     var son = new Son();
     var father = new Father();
     console.log(son.name) //李
    console.log(father.name) //李
    Son.prototype.age = "18";
     console.log(son.age) //18
    console.log(Father.prototype.age) //undefined

    var inner = (function () { //用立即执行函数 形成闭包
        var F = function () {} //用闭包执行变量私有化
        return function (Target,Origin) {
            F.prototype = Origin.prototype;
            Target.prototype = new F();
            Target.prototype.constructor = Target;
            Target.prototype.uber = Origin.prototype;
        }
        }())
    Father.prototype.name = "李";
     function Father() {}
     function Son() {}
     inner(Son,Father);
     var son = new Son();
     var father = new Father();
     console.log(son.name) //李
    console.log(father.name) //李
    /**
     * 命名空间(运用闭包)
     */
    var name = "bcd";
    var init = (function(){
        var name = "abc";
        function callName() {
            console.log(name)
        }
        return function () {
            callName()
        }
    }())
init() //abc
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值