JS : 面向对象-属性判断-call()-apply()-bi

2017-07-23  本文已影响0人  Dream_丹丹

面向对象

对象:属性 和 方法

写面向对象的小窍门

属性判断:判断某个属性,是否在某个对象上

循环遍历

关于Object和Function之间的关系

函数的三种角色

Function.prototype

call apply bind

this小总结

继承

<script>
    function F(){
        this.x = 100;
        this.y = 200;
    }
    F.prototype.a = 111;
    F.prototype.showX = function(){};
    var f1 = new F;
    console.dir(f1);
    function Son(){
        F.call(this);
    }
    var s1 = new Son;
    console.dir(s1);
</script>
<script>
    // 通过此方法继承的公有在第一个__proto__上
    function extend(obj1,obj2) {
        for(var attr in obj2){
            obj1[attr] = obj2[attr];
        }
        return obj1;
    }
    function F() {
        this.x = 100;
        this.y = 100;
    }
    F.prototype.a = 123;
    F.prototype.showX = function(){};
    var f1 = new F;
    console.dir(f1);
    function S(){}
    extend(S.prototype,F.prototype);
    var s1 = new S;
    console.dir(s1);
</script>
<script>
    //通过此方法继承,公有在__proto__找到的__proto__上
   function extend(obj){
        function Tmp() {}
        Tmp.prototype = obj.prototype;
        return new Tmp;
    }
    function F(){
        this.x = 100;
        this.y = 200;
    }
    F.prototype.a = 111;
    F.prototype.showX = function(){};
    var f1 = new F;
    console.dir(f1);
    function S(){};
    S.prototype = new F;
    var s1 = new S;
    console.dir(s1);
</script>
    // 最后S继承的属性位置和F一样
    function extend(obj1,obj2) {
        for(var attr in obj2){
            obj1[attr] = obj2[attr];
        }
        return obj1;
    }
    function F() {
        this.x = 100;
        this.y = 100;
    }
    F.prototype.a = 123;
    F.prototype.showX = function(){};
    var f1 = new F;
    console.dir(f1);

    function S(){
        F.call(this);
    }
    extend(S.prototype,F.prototype);
    var s1 = new S;
    console.dir(s1);
    function setCreat(obj){
        function Tmp(){}
        Tmp.prototype = obj.prototype;
        return new Tmp();
    }
    function F() {
        this.a = 1;
        this.b = 2;
    }
    F.prototype.c = 3;
    F.prototype.d = function () {};
    var f1 = new F;
    console.dir(f1);

    function Son(){
        F.call(this);
    }
    Son.prototype = setCreat(F);
    var s1 = new Son;
    console.dir(s1);

可视区的宽度和高度

上一篇 下一篇

猜你喜欢

热点阅读