面向对象
function Cat(n, c) {
this.name = n;
this.color = c;
this.trait = function () {
alert('卖萌~');
};
};
Cat.prototype.skill = function () {
alert('抓老鼠');
};
Cat.prototype.abc = 123;
function Dog(f,n, c) {//子类
this.food = f;
Cat.call(this,n,c);
};
// Dog.prototype = Object.create(Cat.prototype); ??
var dog1 = new Dog('aa','bb','cc');
console.log(dog1.name); //结果: bb
dog1.trait(); // 结果: 卖萌
//面向委托
var Cat = {
init: function (n,c){
this.name = n;
this.color= c;
},
train: function(){
alert('卖萌~');
},
skill: function(){
alert('抓老鼠');
}
}
var Dog = Object .create(Cat);
Dog.setArr = function (f,n,c){
this.init(n,c)
this.food= f;
};
var dog1 = Object.create(Dog);
dog1.setArr('aa','bb','cc');
console.log(dog1.name);
dog1.skill();
var obj1 = {a:1, b:2};
var obj2 = {
name:'xm',
age:18,
say: function(){
alert('xxxx');
}
}
obj1.c = 3; //设置属性 -> set()
obj1.a = 4;//修改属性 ->set()
console.log(obj1.b); //访问属性 -> get()
Object.defineProperty(obj1,'a',{
// 是否可配置(删除,枚举,读写);
configurable:true,
//是否可枚举(for in 遍历)
enumerable: true,
//是否可写
writable: true,
//属性值
value: 5
});
console.log(obj1.a) // 结果: 5
delete obj1.a;
for(var key in obj1){
console.log(key);
}
显示声明与隐式声明变量的区别?