zoukankan      html  css  js  c++  java
  • JavaScript高级程序设计13.pdf

    使用hasOwnProperty()方法检测一个属性存在实例还是原形中,当属性存在对象实例中时,返回true

    alert(person1.hasOwnProperty("name"));  //false 来自原型

    原型和in操作符

    单独使用和for-in中,单独使用时无论属性存在于实例中还是原型中都返回true

    ECMAScript 5的Object.keys()方法,接收一个对象作为参数,返回一个包含所有可枚举属性的字符串数组(参数是实例则返回实例的属性,是原型则返回原型的属性)

    var keys=Object.keys(Person.prototype);

    alert(keys);  //"name,age,job,sayName"

    得到所有实例属性,无论它是否可枚举用Object.getOwnPropertyNames()方法

    var keys=Object.getOwnPropertyNames(Person.prototype);

    alert(keys);

    用一个包含所有属性和方法的对象字面量来重写整个原型对象

    function Person(){};

    Person.prototype={

        name:"Nicholas",

        age:29,

        job:"Software Engineer",

        sayName:function(){

          alert(this.name);

          }

    };

    因为我们完全重写了原型对象,因此它的constructor属性不再指向Person了,而是指向Object构造函数,此时通过constructor已经无法确定对象的类型了

    var friend=new Person();

    alert(friend instanceOf Object);  //true继承自Object

    alert(friend instanceOf Person);  //true

    alert(friend.constructor ==Person);  //false

    alert(friend.constructor ==Object);  //true

    如果constructor的值很重要
    function Person(){}

    Person.prototype={

        constructor:Person,

        name:"Nicholas"

        };  //注意,这样操作以后会导致它的[[Enumerable]]特性设置为true,变为可枚举属性

      //重写prototype

    Person.prototype={

        name:"Nicholas",

        age:29

        };

      //添加prototype属性

    Person.prototype.sayHi=function(){

      alert("hi");

    };

    由于原型的动态性,我们对原型的任何修改能在实例中反映出来

    通过构造函数创建一个实例后,这个实例的[[Prototype]]指针指向最初原型,如果重写整个原型对象会切断这个实例与原型之间的联系,造成错误

    原型模式的重要性不见体现在创建自定义类型方面,原生引用类型(Object、Array、String)等等都在其构造函数的原型上定义了方法

    alert(typeof Array.prototype.sort);  //"function"

    可以自定义方法

    String.prototype.startsWith=function (text){

        return this.indexOf(text)==0;

    };

    var msg="hello world!";

    alert(msg.startsWith("hello"));  //true

    原型模式的共享是一个优点,却也是一个问题,假如我们通过实例修改原型那么另一个指向这个原型的实例也会反应出这样的修改

    组合使用构造函数模式和原型模式,构造函数模式存私有属性,原型模式存共有属性

    function Person(name,age,job){

        this.name=name;

        this.age=age;

        this.job=job;

        this.friend=["ShelBy","Court"];

        }

    Person.prototype={

        constructor:Person,

        sayName:function(){

          alert(this.name);

            }

    }

    var person1=new Person("Nicholas",29,"Software Engineer");

    var person2=new Person("Greg",27,"Doctor");

    person1.friends.push("Van");

    alert(person1.friends);  //"ShelBy,Court,Van"

    alert(person2.friends);  //"ShelBy,Court"

    动态原型模式

    在构造函数中封装所有信息,后面再检测某个方法是否有效,无效的话再次添加到原型中

    寄生构造函数模式

    创建一个对象,封装对象的代码,返回新创建的对象,在调用本模式时加new操作符

    稳妥构造函数

    不引用this,不使用new调用构造函数,返回对象

  • 相关阅读:
    [转载]Javascript:history.go()和history.back()的用法和区别
    微信商户平台,开通企业付款到用户功能
    使用ASP.Net WebAPI构建REST服务——客户端
    在WebAPI使用Session
    在asp.net一般应用程序中使用session
    Web Api Post注意事项
    WebAPI返回数据类型解惑
    c#中const与readonly区别
    sql之left join、right join、inner join的区别
    C#实现json的序列化和反序列化
  • 原文地址:https://www.cnblogs.com/sdgjytu/p/3720896.html
Copyright © 2011-2022 走看看