Object 类型:
如果你定义了一个Object类型的,如
Cat = {
name: 'blueCat',
init: function() {
}
}
这样是无法通过new Cat()来实例化出一个对象的,这种写法只能用来定义类似类方法或常量,用法如Cat.name, Cat.init()
Function类型:
可以通过new来创建实例的,再借助prototype就可定义实例属性,方法等,例子如下:
Cat = function(initVal) {
this.init(initVal);
}
Cat.prototype = {
instanceVal: 'val1', //
init: function(initVal) {
this.instanceVal = initVal;
console.log('call init');
},
someFunc: function() {
console.log('call someFunc:', this.instanceVal);
}
}
var blackCat = new Cat('black');
console.log(blackCat.someFunc());
var whiteCat = new Cat('white');
console.log(whiteCat.someFunc());