命名空间
2020-01-23 本文已影响0人
山猪打不过家猪
管理变量,防止全局污染
使用闭包,管理变量空间
var name = '123123';
var init = (function(){
var name = 'abc';
function callName(){
console.log(name);
}
return function(){
callName();
}
}());
init();
实现连续调用
var deng = {
smoke:function (){
console.log('Deng smoking...');
return this;
},
drink:function (){
console.log('Deng drinking...');
return this;
},
}
deng.smoke().drink().smoke();
属性拼接
var deng = {
wife1:{name:'xiao1'},
wife1:{name:'xiao2'},
wife1:{name:'xiao3'},
sayWife: function(num){
return this['wife'+num]
}
}
deng.sayWife(1)
对象枚举
1)直接遍历
var obj ={
name:'123',
age:12,
sex:'male',
__proto__:{
lastName:'deng',
}
}
for(var prop in obj){ //prop是属性名
console.log(obj[prop]) ;//枚举里面只能这么写
}
//如果写成,obj.prop会转为 obj['prop']去查找这属性名
2)判断原型
var obj ={
name:'123',
age:12,
sex:'male',
__proto__:{
lastName:'deng',
}
}
for(var prop in obj){
if(obj.hasOwnProperty(prop)){
console.log(obj[prop]);
}
}