Professional JS(6.2.4-6.3.1)组合使用构造函数模式&原型模式---原型链

来源:互联网 发布:设置php.ini路径 编辑:程序博客网 时间:2024/06/07 12:03

Well,if The Flash were my son,l’d tell him a few things.First off,l’d tell him it’s a dangerous world,so be careful.Then l’d tell him he’s a hero.And he’s saving a lot of lives.But the most important thing for him to know,l feel,is that his father’s proud of him.

0.

var msg='Hello world';msg.indexOf('Hello world');//0msg.indexOf('Hello worl');//0msg.indexOf('Hello wor');//0msg.indexOf('Hello wo');//0msg.indexOf('Hello w');//0msg.indexOf('Hello ');//0msg.indexOf('Hello');//0msg.indexOf('Hell');//0msg.indexOf('Hel');//0msg.indexOf('He');//0msg.indexOf('H');//0msg.indexOf('');//0msg.indexOf('Hello world ');//-1

1.不推荐修改原生对象的原型,即使能满足特定的功能,也会可能会导致今后工程的命名冲突问题。
如:String.prototype.startsWith()

2.原型对象最大的问题——–强大的共享性—–原型对象中的属性能在不同实例中得到“映射”。

function Person(){}Person.prototype={    friends:['asan','qin']};var person1=new Person();var person2=new Person();person1.friends.push('Greg');person1.friends;//["asan", "qin", "Greg"]person2.friends;//["asan", "qin", "Greg"]person1.friends==person2.friends;//true

3.组合使用构造函数模式(定义实例属性)&原型模式(定义方法和共享属性)

//构造函数模型function Person(name,age,job){    this.name=name;    this.age=age;    this.job=job;    this.friends=['asan','qin'];}//原型模型Person.prototype={    constructor:Person,    sayName:function(){        return this.name;    }};var person1=new Person('yyc',21,'Front-end Enginner');var person2=new Person('Greg',20,'student');person1.friends.push('xxx');person1.friends;//["asan", "qin", "xxx"]person2.friends;//["asan", "qin"]person1.friends==person2.friends;//falseperson1.sayName==person2.sayName;//true

4.动态原型模式:将原型模式“封装”入构造函数模式。

unction Person(name,age,job){    this.name=name;    this.age=age;    this.job=job;    if(typeof this.sayName!='function'){        Person.prototype.sayName=function(){            return this.name;        };    }}var friend=new Person('Greg',20,'student');friend.sayName();//"Greg"

5.假设要创建一个具有额外方法的特殊数组。

function SpecialArray(){    var values=new Array();//构造函数创建数组    values.push.apply(values,arguments);//用构造函数中接受到的参数来初始化values这个数组    values.toPipedString=function(){        return this.join('|');    };//添加方法    return values;//返回数组}var colors=new SpecialArray('red','green','blue');colors.toPipedString();//"red|green|blue"

6.(5)寄生构造函数模式:返回的对象与构造函数或者与构造函数的原型属性之间没有任何联系。

7.就算你知道了对象类型,有什么用?对象类型有哪几种?
①具体的情况下需分析对应的对象类型,来执行特定的操作。
②四种识别对象类型的方法:

a)typeof,返回的类型有:”undefined”,”number”,”boolean”,”string”,”function”,”object”
优点:typeof(null);//”object”—var z;typeof(z);//”undefined”—“undefined”的应用
缺点:无法具体识别object中的类型。

b)instanceof,
返回的类型有:Number,String,Boolean,Object,Function,Date,RegExp,Array
优点:能区分更细化的object类型。
缺点:“确定答案的作用”,必须使用构造函数表示法

c)constructor
优点:可以返回继承的类型
缺点:实例的constructor属性不再指向构造函数,但可人共设置。

d)Object.prototype.toString.call();
优点:通用,返回具体的对象类型,[object,xxx]
缺点:不能返回继承的类型。

转自:http://www.cnblogs.com/java-class/p/4726192.html#undefined

8.稳妥构造函数模式(durable objects)—-安全执行环境
在寄生构造函数模式(构造函数+工厂模式【函数中(创建对象、对象的属性和方法,返回该对象)】)的基础上,去掉this&new。

function Person(name,age,job){    var o=new Object();    o.sayName=function(){        return name;//去掉this    };    return o;}var friend=Person('Greg',20,'student');//去掉newfriend.sayName();//"Greg"

9.instanceof操作符,对使用寄生构造函数模式和稳妥构造函数模式产生的对象无效。

10.签名:
In Computer science,a type signature or type annotation defines the inputs and outputs for a function,subroutine or method.A type signature includes the number of arguments,the types of arguments and the order of the arguments contained by a function.A type signature is typically used during overload resolution for choosing the correct definition of a function to be called among many
overloaded forms.—–Wikipedia

11.原型链 —大工程

function SuperType(){    this.property=true;}SuperType.prototype.getSuperValue=function(){    return this.property;};function SubType(){    this.subproperty=false;}/*SubType.prototype作为SuperType类型的一个实例----继承了SuperType.继承:通过原型,使得一个引用类型获得另一个引用类型(实例)的属性和方法。*/SubType.prototype=new SuperType();SubType.prototype.getSubValue=function(){    return this.subproperty;};var instance=new SubType();instance.getSuperValue();//true

12.所有函数的默认原型都存在一个内部指针指向Object.prototype.

13.确定原型与实例的两种方式:
①instanceof—–instance instanceof Object/SuperType/SubType–从而确定原型链—-一个实例可能对应多个原型;

②isPrototypeOf()—-Object/SuperType/SubType.prototype.isPrototypeOf(instance);

阅读全文
0 0
原创粉丝点击