渡一—— 12-2 继承模式,命名空间,对象枚举(下)

命名空间
管理变量,防止污染全局,适用于模块化开发

对象方法

var org = {

    department1 : {
        jicheng : {
            name : "abc",
            age : 123
        },
        xuming : {

        }
    },
    department2 : {
        zhangsan : {

        },
        lisi : {

        }
    }
}
// org.department1.jicheng.name
var jicheng = org.department1.jicheng
jicheng.name

2,闭包 模块化开发
// 现在用工具类webpack,groud

var name = "bbb"
var init = (function(){

    var name = "abc";
    function callName(){
        console.log(name);
    }
    return function(){
        callName();
    }


}())

init() //abc
var initDeng = (function(){

    var name = 123;
    function callName(){
        console.log(name);
    }
    return function(){
        callName();
    }

}())

initDeng();

链式调用.css().html().css()

var deng = {

    smoke : function (){
        console.log("smoking,...xuan cool!!!");
        return this;
    },
    drink : function (){
        console.log("drinking...,ye cool!");
        return this;
    },
    perm : function(){
        console.log("preming...,cool!");
        return this;
    }

}

deng.smoke();
deng.drink();
deng.prem();

deng.smoke().drink().prem().smoke();

属性的表示方法

系统默认是obj['name']

var obj = {
    name : "abc"
}
obj.name     //abc
obj['name']  //abc
obj.name --> obj['name']


var deng = {
    wife1 : {name : "xiaoliu"},
    wife2 : {name : "xiaozhang"},
    wife3 : {name : "xiaomeng"},
    wife4 : {name : "xiaowang"},
    sayWife : function(num){
        /*switch(num){
            case 1:
                return this.wife1;
            case 2:
                return this.wife2;
        }*/
        return this['wife'+num]


    }

}

对象的枚举
for in

for in 上的方法

1.hasOwnProperty
2.in
3.instanceof

var arr = [1,3,3,4,5,6,6,7,7];
//遍历 枚举 enumeration
for(var i=0;i<arr.length;i++){
    console.log(arr[i]);
}

//遍历对象
var obj = {
    name : "aaa",
    age : 123,
    sex : "male",
    height : 180,
    wight : 75,
}

for(var prop in obj){
    console.log(prop + '*' + typeof(prop)); //name*string age*string...
    conso.log(obj.prop) //-->obj['prop'] 打印5个undefined
    conso.log(obj[prop]) //"aaa" 123...
}

var obj1 = {
    a : 123,
    b : 234,
    c : 345,
}

for(var prop in obj1){
    console.log(obj.prop++)
}

hasOwnProperty 排除原型上的属性
作用:不想显示__proto__上的方法

var obj = {
    name : "aaa",
    age : 123,
    sex : "male",
    height : 180,
    wight : 75,
    __proto__:{
        lastName:"deng"
    }
}

for(var prop in obj){
    if(obj.hasOwnProperty(prop)){
        conso.log(obj[prop]) //不显示lastName
    }
    
}

in查看属性是不是对象的,包括自己的和原型父级的属性(没什么用)

'height' in obj //true
'lastName' in obj //true

instanceof

A instanceof B

对象 是不是 B构造函数构造出来的
看A对象的原型链上 有没有 B的原型

function Person(){}
var person = new Person();

person instanceof Person //true
person instanceof object //true
[] instanceof Array        //true
[] instanceof object    //true

typeof([]) //object
typeof({}) //object

判断是数组或是对象

//1
var obj={};
obj.constructor //function Object();
[].constructor  //function Array();

//2
[].instanceof Array //true
obj instanceof Array //false

//3
toString
[1,2,3].toString -->Number.toString()-->'1,2,3'
Object.prototype.toString.call([]) -->Object.toString()-->'[object Array]'
Object.prototype.toString.call(123) '[object Number]'
原文地址:https://www.cnblogs.com/lisa2544/p/15318847.html