工厂模式&new

工厂模式

用函数构造一个对象,并且返回这个对象,叫做工厂模式(可以批量生产)。

工厂模式虽然解决了创建 多个相似对象的问题,但却没有解决对象识别的问题(即怎样知道一个对象的类型)

1
2
3
4
5
6
7
8
9
function Dog(name,age){
var dogs={}
dogs.name=name
dogs.age=age
dogs.leg=4
dogs.type='短腿'
return dogs
}
Dog('tom',6) // Object {name: "tom", age: 6, leg: 4, type: "短腿"}

构造函数模式(即new)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function Dog(name,age){
var dogs={} ----1.new帮忙创建临时对象
dogs.name=name
dogs.age=age
dogs.__proto__=Rdogs -----2.new帮忙绑定原型,并同意叫做prototype
return dogs -----3.帮忙return
}
Rdogs={
leg:4,
type:'短腿'
}
Dog('tom',18)
-----------------------------使用new,把共有的特性放在原型链上,私有的属性放在构造函数上
function Dog(name,age){ -----构造函数一般函数名首字母大写
this.name=name ------构造函数依赖 new、this
this.age=age -----构造函数什么都不返回,只有new才能帮它return对象
}
Dog.prototype={
leg:4,
type:'短腿'
}
new Dog('tom',18)
  • debugger一下,可以看到this已经有共有属性了,这是因为下面定义对象的时候内存里马上就赋值了,上面的是函数,等着调用才会去执行。

  • new会帮你return,但是如果自己写上return的话,会怎么样?(new会帮你return this,在new之前的构造函数里加了自己的return,那么new执行不到,所以构造函数不要瞎return)

  • 如果构造函数里自己写了return this,而调用的时候不写new,会出现什么呢?(直接调用一个函数,this就是window)

    生成对象并且改变this(帮你生成一个对象,this指向这个对象)

  • 如果我不想加 new,但是又想实现跟 new 一样的功能(加条件判断,帮忙加new)

    jQuery是一个构造函数,返回一个对象,返回的对象有原型,所以它有new,做了以上类似的处理,所以我们调用的时候不需要些new

  • new会帮忙添加constructor属性

    如果像上面那样写Dog.prototype={},这样就引向了另外一块地址,把原来的constructor属性覆盖了,所以推荐把constructor属性加上去constructor:Dog