zoukankan      html  css  js  c++  java
  • javascript原型

    每个函数或对象都有一个原型属性指向一个原型对象,除特殊除外

    构造函数创建的对象中__proto__属性都指向同一个原型对象,共享这个原型对象

    相当于java中static,所有对象都共享原型对象

                function Box(){}
                //每个函数或对象都有一个原型对象,除特殊除外
                //原型相当于static,所有对象都共享
                Box.prototype.name = '张三';
                
                Box.prototype.run = function(){
                    
                    return this.name;
                }
                
                var box1 = new Box();
                var box2 = new Box();
                console.info(box1.run()==box2.run());//true

    __proto__指向其构造函数Object的prototype;

    box1. __proto__

    IE中不支持这个属性

    isPrototypeOf是用来判断指定对象object1是否存在于另一个对象object2的原型链中,是则返回true,否则返回false。
    格式如下:
    object1.isPrototypeOf(object2);


    console.info(Box.prototype.isPrototypeOf(box1));

    object.hasOwnProperty('proName'); //实例中是否有属性,不是原型属性

    //property in object   不管实例中有该属性或者原型中有该属性,只要一个有就返回true

    //判断只有原型中有属性
                function isProperty(object,property){
                    
                    return !object.hasOwnProperty(property)&&(property in object);
                    
                }

    //使用字面量的方式创建对象
                function Boxp (){}
                
                Boxp.prototype = {
                    name:"张三",
                    add : function(){
                        
                    },
                    
                    constructor:Boxp//把他的构造强制指回Boxp
                    
                }
    //用原型扩展方法
                String.prototype.addme = function(){
                    
                    return this+"自己扩展";
                }
                
                console.info('aaaaa'.addme());
    //动态原型模式,把原型和构造函数放到一起形成一个整体
                function People(name){
                    this.name = name;
                    //new 的时候只初始化一次,增加效率
                    if(typeof this.eat != 'function'){
                        //把实例中共享的写到原型中,节省内存
                        People.prototype.eat = function(){
                            return this.name;
                        }
                    }
                    
                }
    //工厂模式
                function P(name){
                    var obj = new Object();
                    obj.name = name;
                    obj.eat = function(){
                        return this.name;
                    }
                    return obj;
                }
                
                var p1 = P("张三");
                alert(p1.eat());

    javascript  不支持接口,通过原型链实现继承

    P.prototype.name = "张三";
                
    var p0 = new P();
                
    alert(p0.constructor);
                
                
    function PP(){
                    
    }
                
    PP.prototype = {
                name : "张三2"
                   //constructor:PP   //通过字面量方式创建原型对象需把构造指回原来对象PP,否则为Object
                }
                
    var pp0 = new PP();
    alert(pp0.constructor);

     原型字面量方式创建对象,重写后切断原来的原型对象

                function Parent(){
                    this.name="张三";
                }
                
                function Child(){
                    this.age = 10;
                }
                
                Child.prototype = new Parent();
                
                var child = new Child();
                
                console.info(child.name);

    对象冒充继承不了对象的原型,构造放大方法里不会被共享

             //组合继承
                
        function Parent1(name,age){
            this.name = name;
            this.age = age;
        }
        //共享一个方法,省内存
        Parent1.prototype.run = function(){
                    
            return this.name+this.age;
        }
                
            function Child1(name,age){
               Parent1.call(this,name,age);
                    
            }
                
            Child1.prototype = new Parent1();
                
            var child1 = new Parent1('张三',10);
               
            console.info(child1.run());

     构造函数中的实例属性,和原型中的属性如果重名,会先从实例中找,找不到再到原型中找

                function P(){
                    this.name = "李四"
                }
                
                P.prototype.name = "张三";
                
                var p0 = new P();
                
                alert(p0.name);
    //对象冒充实现继承,不能继承原型
                
                function Parent(name){
                    
                    this.name = name;
                    
                }
                
                function Child(name){
                    
                    Parent.call(this,name);
                    
                }
    //组合继承,用call继承实例,用原型继承   继承原型中的方法,常用的模式
                function Parent(name){
                    
                    this.name = name;
                    
                }
                Parent.prototype.eat = function(){
                    
                    return this.name;
                }
                
                function Child(name){
                    
                    Parent.call(this,name);
                    
                }
                Child.prototype = new Parent();
  • 相关阅读:
    mysql分组统计后将结果顺序排列(union实现)
    mysql格式化日期
    yaf框架安装
    如何通过PHP将excel的数据导入MySQL中
    yii日志保存机制
    安装PyInstaller打包python
    python正则表达式详解
    Python中类的定义与使用
    例子 使用sqlite3 数据库建立数据方式
    python操作轻量级数据库
  • 原文地址:https://www.cnblogs.com/jentary/p/6279958.html
Copyright © 2011-2022 走看看