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>

posted @ 2017-11-26 19:40  KioWu  阅读(106)  评论(0编辑  收藏  举报