js中的Object对象

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>

           var  obj={
               name:'zain',
               age:26
           };

           Object.defineProperty(obj,"__abc__",{
                value:2,
                configurable:false,          //是否可配置
                enumerable:false,          //是否可枚举,迭代
                writable:false               //是否可以改变值
           });



       Object.defineProperty(obj,"show",{
          
          configurable:false,          //是否可配置
          enumerable:true,          //是否可枚举,迭代
          writable:true
                       //是否可以改变值
     });
        //    var __a__={};      //这个对象最好别操作,私有受保护的。
        //    var __DEFS__={};   // __代表受保护,全大写代表常量

        //    console.log(obj);





        // Object.keys
        // Object.getOwnPropertyNames
        // Object.getOwnPropertyDescriptor
        // Object.propertyIsEnumerable
        // Object.setPrototypeOf
        // Object.getPrototypeOf
        // Object.hasOwnProperty


//返回该对象可枚举的属性
        var keys=Object.keys(obj);
         console.log(keys);


//返回该对象所有的属性,__proto__除外。
    var names=Object.getOwnPropertyNames(obj);
    console.log(names);
 
//返回对象的原型
 console.log(Object.getPrototypeOf(obj));

//JavaScript对象既可以有自己的属性,又可以从原型对象继承属性。 hasOwnProperty()方法提供了区分继承属性和非继承的局部属性的方法。
console.log(obj.hasOwnProperty('age'),obj.hasOwnProperty('Jack'),obj.hasOwnProperty('toString'));



//将一个指定的对象的原型设置为另一个对象或者null(既对象的[[Prototype]]内部属性).
//Object.setPrototypeOf(obj, prototype)
console.log(Object.setPrototypeOf(obj,{name:'zitming'}));
console.log(obj.name);              //还是zain,这个方法只是设置的原型对象,__proto__上的name为zitming


//返回对象的原型,{name:'zitming'}
console.log(Object.getPrototypeOf(obj));


//表示这个对象是否可枚举(迭代)
console.log(obj.propertyIsEnumerable('name'));


//获取指定对象的自身属性描述符。自身属性描述符是指直接在对象上定义(而非从对象的原型继承)的描述符
//Object.getOwnPropertyDescriptor(object, propertyname)
console.log(Object.getOwnPropertyDescriptor(obj,'name'));


//  function outer(){
//      inner();
//      console.log(outer.caller);
//  }

//  function inner(){
//      console.log(inner.caller);
//  }


//  (function abc(){
//      outer()
//  })();
    </script>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/zhumeiming/p/9711104.html