通过原型和字面量的方法去创建对象

通过原型和字面量的方法去创建对象

function Cat(name,age){
	this.name = name;
	this.age = age;
}
Cat.prototype.sayName = function(){
	alert(this.name)
	} 
var cat = new Cat("five",18); //实例化


//原型模式执行顺序先在已经实例化的构造函数里去找,如果没有就去原型对象里去找。
//总而言之就是就近原则

在这里插入图片描述

字面量:

function Cat(name,five){
this.name = name;
this.age = age;
}
Cat.prototype = {
	sayName = function(){
		alert(this.name)
	}
}
var cat1 = new Cat(five,17);
cat1.sayName()

//通过字面量的方式去创建的对象可以传入自定义的参数,也能引用原型

猜你喜欢

转载自blog.csdn.net/qq_33200168/article/details/83109689
今日推荐