js创建对象的方式;
1字面量创建方式
var obj={}
2构造函数创建对象
var obj = new Object()
3工厂模式创建对象
为什么使用工厂模式和构造函数
虽然Object构造函数或者字面量方式可以创建多个对象,但是有着一个明显得缺点那就是重复造轮子,产生大量重复得代码,为了解决这个问题就出现啦工厂模式和构造函数模式。
什么是工厂模式? 它有什么优缺点
1顾名思义,工厂模式都是像工厂一样创建对象,这样的理解有点欠妥,说的高大上一点,工厂模式其实是软件领域中一种广为人知得一种设计模式,这种模式抽象了创建对象得具体过程。开发人员发明一种函数,是用函数大量创建对象的方法;
2下面用一段代码告诉你什么是工厂模式;、
function createPerson(name,age){
var obj = new Object();
obj.name = name;
obj.age = age;
obj.say =function(){
alert(this.name)
}
return obj
}
var person = createPerson("wkm",20)
console.log(person) // 输出参建得对象
person.say() //wkm
3 优缺点
优点
可以创建多个相似得对象
缺点
1:不能解决对象得识别问题(不知道这个对象得类型)
2:但是每次创建新对象得时候所有得say方法都是一样的,但是储存啦多次浪费资源;所以就出现了下满得构造函数创建对象;
4构造函数创建对象
function Person(name,age){
this.name = name,
this.age = age,
this.say = function (){
alert(this.name)
}
}
var person = new Person("wkm",18)
console.log(person) //打印出一个对象
person.say()//wkm
构造函数创建对象其实经历四个步骤;
1 创建了一个新的对象
2 将构造函数得作用域赋给了新对象(改变啦this得指向)
3 执行构造函数里的代码;
4 返回新的对象
构造函数摸手机在最后隐士得返回return this缺少得情况下,会将属性和方法添加给全局对象,浏览器端会添加给window对象,可以根据
return this 得特征调用call apply 指定this;
构造函数创建对象的缺点
创建自定义得构造函数意味着可以将实例标识为一种特定类,这是构造函数比工厂模式强的地方,因为知道他从哪里来是谁生产出来的。
缺点:所有的实例对象都可以继承构造器函数中的属性和方法。但是,同一个对象实例之间,无法共享属性
解决方法
a:所有实例都会通过原型链引用到prototype
b:prototype相当于特定类型所有实例都可以访问到的一个公共容器
c:那么我们就将重复的东西放到公共容易就好了
5原型模式创建对象
function Person(){};
Person.prototype.name="wkm";
Person.prototype.say=function() {
alert(this.name)
}
var person = new Person()
console.log(person); //输出对象
person.say(); //wkm
console.log(person.name) //wkm
优缺点;
实现了方法和属性得共享,可以动态得添加对象得属性和方法,但是没办办法创建自己得实例实例和方法,没有办法传参;
6构造函数和原型组合创建对象
function Person(name, age) {
this.name = name
this.age = age
}
Person.prototype.say = function() {
console.log(this.name)
}
var person = new Person('hello',18)
console.log(person) //输出对象
console.log(person.age)// 18
比较常用;
接下来说继承
1最简单得继承方式 原型链继承
function Animal() {}
Animal.prototype.name = 'cat'
Animal.prototype.age = 1
Animal.prototype.say = function() {console.log('hello')}
var cat = new Animal()
cat.name // cat
cat.age // 1
cat.say() // hello
最简单得继承方式但是也有它得原理和缺点
原理:来自原型对象上的所有属性能被它得实例所共享
缺点
1:来自原型对象上的所有属性能被所有的实例共享
2:创建子类实例无法向父类构造函数传参数;
3:要想为子类添加实例和方法必须在new 之后无法直接放在函数构造器里;
针对缺点1举例子说明
function Parent () {
this.names = ['kevin', 'daisy'];
}
function Child () {
}
Child.prototype = new Parent();
var child1 = new Child();
child1.names.push('yayu');
console.log(child1.names); // ["kevin", "daisy", "yayu"]
var child2 = new Child();
console.log(child2.names); // ["kevin", "daisy", "yayu"]
2构造函数继承(经典继承)
function Parent () {
this.names = ['kevin', 'daisy'];
}
function Child () {
Parent.call(this);
}
var child1 = new Child();
child1.names.push('yayu');
console.log(child1.names); // ["kevin", "daisy", "yayu"]
var child2 = new Child();
console.log(child2.names); // ["kevin", "daisy"]
原理;使用call或apply方法,将父对象的构造函数绑定在子对象上.
优点:
1 :避免引用类型的实例被所有类型共享;
2:子类实例可以向父类构造函数传参数;
举例
function Parent (name) {
this.name = name;
}
function Child (name) {
Parent.call(this, name);
}
var child1 = new Child('kevin');
console.log(child1.name); // kevin
var child2 = new Child('daisy');
console.log(child2.name); // daisy
缺点:
方法都在构造函数中定义,每次创建实例都会创建一遍方法。
3组合继承
原型链继承和经典继承双剑合并
function Parent (name) {
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
Parent.prototype.getName = function () {
console.log(this.name)
}
function Child (name, age) {
Parent.call(this, name);
this.age = age;
}
Child.prototype = new Parent();
var child1 = new Child('kevin', '18');
child1.colors.push('black');
console.log(child1.name); // kevin
console.log(child1.age); // 18
console.log(child1.colors); // ["red", "blue", "green", "black"]
var child2 = new Child('daisy', '20');
console.log(child2.name); // daisy
console.log(child2.age); // 20
console.log(child2.colors); // ["red", "blue", "green"]
优点:融合原型链继承和构造函数继承得优点,是js中最常见得继承方式;
4原型继承
function createObj(o) {
function F(){}
F.prototype = o;
return new F();
}
原理:ES5 Object.create 的模拟实现,将传入的对象作为创建的对象的原型。
缺点 :包含引用类型的属性值始终都会共享相应的值,这点跟原型链继承一样。
5寄生式继承
function createObj (o) {
var clone = object.create(o);
clone.sayName = function () {
console.log('hi');
}
return clone;
}
原理: 创建一个仅用于封装继承过程的函数,该函数在内部以某种形式来做增强对象,最后返回对象。
缺点 :跟借用构造函数模式一样,每次创建对象都会创建一遍方法。
6寄生式组合继承
为了便于大家阅读,在这里重复下组合继承得代码
function Parent (name) {
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
Parent.prototype.getName = function () {
console.log(this.name)
}
function Child (name, age) {
Parent.call(this, name);
this.age = age;
}
Child.prototype = new Parent();
var child1 = new Child('kevin', '18');
console.log(child1)
组合继承最大的缺点是会调用两次父构造函数。
一次是设置子类型实例的原型的时候:
Child.prototype = new Parent();
一次在创建子类型实例的时候:
var child1 = new Child('kevin', '18');
回想下 new 的模拟实现,其实在这句中,我们会执行:
Parent.call(this, name);
在这里,我们又会调用了一次 Parent 构造函数。
所以,在这个例子中,如果我们打印 child1 对象,我们会发现 Child.prototype 和 child1 都有一个属性为colors,
属性值为['red', 'blue', 'green']。
那么我们该如何精益求精,避免这一次重复调用呢?
如果我们不使用 Child.prototype = new Parent() ,而是间接的让 Child.prototype 访问到 Parent.prototype 呢?
看看如何实现:
function Parent (name) {
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
Parent.prototype.getName = function () {
console.log(this.name)
}
function Child (name, age) {
Parent.call(this, name);
this.age = age;
}
// 关键的三步
var F = function () {};
F.prototype = Parent.prototype;
Child.prototype = new F();
var child1 = new Child('kevin', '18');
console.log(child1);
最后封装继承得方法:
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
function prototype(child, parent) {
var prototype = object(parent.prototype);
prototype.constructor = child;
child.prototype = prototype;
}
// 当我们使用的时候:
prototype(Child, Parent);
引用《JavaScript高级程序设计》中对寄生组合式继承的夸赞就是:
这种方式的高效率体现它只调用了一次Parent构造函数,并且因此避免了在 Parent.prototype 上面创建不必要的、多余的属性。与此同时,原型链还能保持不变;因此,还能够正常使用 instanceof 和 isPrototypeOf。开发人员普遍认为寄生组合式继承是引用类型最理想的继承范式。