call的理解

<html>
<body>

    <p> 11111</p>
    
    <p> 2222</p>
    <script>
    /*
        函數有prototype屬性
        對象有__proto__字段 ,而這個字段的值,等於此對象
        的構造器(函數)的原型對象(prototype)
    
    */
    //******call的理解*****
    /*
        //this指向誰由運行時
        //this等於.號左邊的東西
        function P(name,age){
            this.name = name;
            this.age = age;
        }
        
        var o = {};
        //P("aaa",22);
        //下面的代碼仍然是調用P函數
        //可是P函數裏面的this等於第一個參數
        //也就是o
        P.call(o,"bbbb",33);
        
        //P(o,"bbb",33);
        //function P(obj,name,age){
        //    obj.name = name;
        //    obj.age = age;
        //}
        console.log(o.name);
        console.log(o.age);
        
        //
        
        */
        
        
        //**************call的應用*****
        //應用1: 讓對象成爲數組型對象
        /*
        var o = {};
        //for(var i = 0;i< 3;i++){
            //o[i] = "asdfasdf";
        
        //}
        //o.length = 3;
        
        
        var allp = document.getElementsByTagName("p");
        
        for(var i =0 ; i< allp.length;i++){
            o[i] = allp[i];
        }
        o.length = allp.length;
        
        console.log(o);
        */
        
        /*
        var data = {};
        Array.prototype.push.call(data,100,200);
        Array.prototype.push.apply(data,[1,2,3,8,10]);
        console.log(data);
        */
        
        /*
        function Person(name,age){
            this.name = name;
            this.age = age;
        }        
        var p = new Person("aa",22);
        
        */
        
        //經過原型繼承
        var parent = {p:"p"};
        var child = {};
        
        
        //prototype 屬性只有函數纔有。
        //__proto__    只要是對象,就有這個。
        
        console.log(parent.prototype);
        
        //child.__proto__ = parent;
        
        //__proto__這個不是ECMA標準的規範。
            
            //因此上面child.__proto__ = parent;這行代碼
            //雖然實現了繼承,但不是標準,也就是不能這樣作。
            
            //child.__proto__ = xxxxx  =yyy;
            //child.__proto__ ===Object.prototype;
        
        //Object.prototype = parent;  //這行代碼理論上是能夠完成繼承
        //可是由於Object.prototype是隻讀,你不能賦值
        //因此不能改。
        
        
            //child.__proto__ = xxxx -》xxx.__proto__  == parent;
            
            
            
            //var
            
            /*
            function Child2(){
            
            }
            Child2.prototype = parent;
            var c2 = new Child2();
            */
            
            /*理解一: 能夠作一個假的繼承
            //會致使子的東西也變成父的東西
            function Animal( name){
                this.name = name;
            }
            Animal.prototype.eat = function(){
                console.log(this.name   + " eating...");
            }
            function Cat( name){
            
                this.name = name;
            }
            
            //會致使子的東西也變成父的東西
            Cat.prototype = Animal.prototype;
            
            Cat.prototype.zhualaoshu = function(){
                console.log("抓老鼠");
            }
            
            var cat1 = new Cat("bosi");
            //Cat是個類,Animal也是個類
            
            */
        
        
        
                //理解二:
            function Animal( name){
                this.name = name;
            }
            Animal.prototype.eat = function(){
                console.log(this.name   + " eating...");
            }
            function Cat( name){
            
                this.name = name;
            }
            
            //會致使子的東西也變成父的東西
            //Cat.prototype = Animal.prototype;
            //Cat.prototype = {};
            //{} = Animal.prototype;
            //{}.__proto__ = Animal.prototype;
            
            //因爲__proto__不能直接使用。
            
            function F(){
            
            }
            F.prototype = Animal.prototype;
            Cat.prototype = new F();
            
            Cat.prototype.zhualaoshu = function(){
                console.log("抓老鼠");
            }
            
            var cat1 = new Cat("bosi");
            //Cat是個類,Animal也是個類
    </script>

</body>

</html>html

相關文章
相關標籤/搜索