原型简单的练习

function C1(name) {
        if (name) {
            this.name = name;
        }
        ;
    }
    function C2(name) {
        this.name = name;
    }
    function C3(name) {
        this.name = name || 'join';
    }
    C1.prototype.name = 'Tom';
    C2.prototype.name = 'Tom';
    C3.prototype.name = 'Tom';
    console.log(new C1().name)//=>'Tom'
    console.log(new C2().name)//=>'undefined'
    console.log(new C3().name)//=>'join'
function fun() {
        this.a = 0;
        this.b = function () {
            alert(this.a);
        }
    }
    fun.prototype = {
        b: function () {
            this.a = 20;
            alert(this.a);
        },
        c: function () {
            this.a = 30;
            alert(this.a)
        }
    }
    var my_fun = new fun();
    my_fun.b();//=>'0'
    my_fun.c();//=>'30'
function Foo() {
         getName = function () {
             console.log(1);
         };
         return this;
     }
     Foo.getName = function () {
        console.log(2);
     };
     Foo.prototype.getName = function () {
        console.log(3);
     };
     var getName = function () {
        console.log(4);
     };
     function getName() {
        console.log(5);
     }

     Foo.getName();//=>2
     getName();//=>4
     Foo().getName();//=>1
     getName();//=>1
     new Foo.getName();//=>2
     new Foo().getName();//=>3
     new new Foo().getName()//=>3
function Fn() {
     this.x = 100;
     this.y = 200;
     this.getX = function () {
     console.log(this.x);
     }
     }
     Fn.prototype.getX = function () {
     console.log(this.x);
     };
     Fn.prototype.getY = function () {
     console.log(this.y);
     };
     var f1 = new Fn;
     var f2 = new Fn;
     console.log(f1.getX === f2.getX);//=>false
     console.log(f1.getY === f2.getY);//=>true
     console.log(f1.__proto__.getY === Fn.prototype.getY);//=>true
     console.log(f1.__proto__.getX === f2.getX);//=>false
     console.log(f1.getX === Fn.prototype.getX);//=>false
     console.log(f1.constructor);//=>Fn(){}
     console.log(Fn.prototype.__proto__.constructor);//=>Object(){}
     f1.getX();//=>100
     f1.__proto__.getX();//=>undefined
     f2.getY();//=>200
     Fn.prototype.getY()//=>undefined

var name = 'zhufengpeixun';
     var Fn =function (name) {
     var name = 'world';
     this.name = 'zhufeng';
     this.sex = function () {
     this.name = 'hello';
     }
     }
     var f1 = new Fn(name);
     var f2 = new Fn('age');
     console.log(f1.name);//=>'zhufeng'
     console.log(f2.age);//=>'undefined'
     f1.sex();
     console.log(f1);{name:zhufeng,sex:fn}
     f1.sex === f2.sex//=>false
     Fn.name == f1.name//=>false
function Fn() {
        var a = 1
        this.a = a
    }
    Fn.prototype.say = function () {
        this.a = 2
    }
    Fn.prototype = new Fn;
    var f1 = new Fn;
    f1.__proto__.b = function () {
        this.a = 3
    };
    console.log(f1.a)//=>1
    console.log(f1.prototype)//=>'undefined'
    console.log(f1.b)//=>'f'
    f1.hasOwnProperty('b')//=>false
    'b' in f1//=>true
    console.log(f1.constructor == Fn)//=>true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值