js闭包及object练习

<script>
//  (function(){
//      var i = 1;
//      getValue = function(){
//          return i;
//      }
//      setValue = function(x){
//              n = x;
//      }
//  })();
//  
//  
//  function test(x){
//      var i = 0;
//      return function(){
//          return x[i++];
//      }
//  }
//  var next = test(['a','b','c','d']);
//  alert(next());a
//  alert(next());b
//  alert(next());c
//  alert(next());d
//  
//  
    
    
//  function f(){
//      var a = [];
//      var i;
//      for (i=0;i<3;i++) {
//          a[i] = function(){
//              return i;
//          }       
//      }
//      return a;
//  }
//  var test = f();
//  alert(test[0]());3
//  alert(test[1]());3
//  alert(test[2]());3
//  
//  
//      
//  function f(){
//      var a = [];
//      var i;
//      for (i=0;i<3;i++) {
//          a[i] = (function(x){闭包函数中包含函数
//              return function(){
//                return x;
//              }
//          })(i)
//      }
//      return a;
//  }
//  var test = f();
//  alert(test[0]());0
//  alert(test[1]());1
//  alert(test[2]());2
//  
    
    
    
//  function f(){
//      function test(x){
//          return function(){
//              return x;
//          }
//      }
//      var a = [];
//      var i;
//      for (i=0;i<3;i++) {
//          a[i]=test(i);
//      }
//      return a;
//  }
//  var res = f();
//  alert(res[0]());0
//  alert(res[1]());1
//  alert(res[2]());2
//  
//  
//  
//  

//var obj = {};
//console.log(Object.isExtensible(obj));检查是否可扩展
//var a = new Date();
//console.log(Object.isExtensible(a));
//
// obj1 = Object.preventExtensions(obj);设置为不可扩展
// console.log(obj1==obj);
// console.log(Object.isExtensible(obj1));

//Object.defineProperty(obj,'z',{value:1});给对象设置属性


//console.log(z);


//var obj = {
//  x:1,
//  y:2,
//  username:'king'
//}
//
//
//obj.age = 12;
//delete obj.x;
//var o = Object.seal(obj);冻结属性
//
//
//console.log(obj.y);
//console.log(obj.x);
//
//
//
//  console.log(Object.isSealed(o));
//  console.log(Object.getOwnPropertyDescriptor(obj,'username'));对象的所有属性
    
//  
//  var obj = {
//      prop:function(){},
//      foo:'king'
//  }
//  obj.test = 'this is a test';
//  delete obj.prop;
//  var o = Object.freeze(obj);浅冻结对象
//  console.log(Object.isFrozen(o));
//  
    
    
//  
//  var obj1 = {
//      internal:{}
//  };
//  Object.freeze(obj1);
//  obj1.internal.x = 1;
//  console.log(obj1.internal.x);
//  深度冻结对象
//  function deepFreeze(obj){
//      var prop,propKey;
//      Object.freeze(obj);
//      for(propKey in obj ){
//          prop = obj[propKey];
//          if(!obj.hasOwnProperty(propKey)||!(typeof prop === 'object')||Object.isFrozen(prop))){
//              continue;
//          }
//          deepFreeze(prop);
//      }
//  }
//  
//1.一个对象默认是可扩展的非冻结的
//2.一个不可扩展的对象同时也是一个冻结的对象 



//
//var arr = ['a','b','c'];

//console.log(Object.getOwnPropertyNames(arr));
list对象上的属性
//
//console.log(Object.keys(arr));
对象属性的键名
//
//
//var  obj = {
//  0:'d',
//  1:'e',
//  2:'f'
//}
//console.log(Object.keys(obj));


////
//var obj1 = Object.create({},{
//  getFoo:{
//      value:function(){
//          return this.foo;
//      }
//  }
//});
//
//obj1.foo = 123;
//console.log(Object.getOwnPropertyNames(obj1));
//console.log(Object.keys(obj1));



//obj = {
//  name:'king',
//  age:12
//}
//
//
//console.log(Object.getOwnPropertyDescriptor(obj,'name'));

//var obj = new Object;
//console.log(obj.constructor == Object);

//var obj1 = new Array;
//console.log(obj1.constructor == Array);



//function Test(){}
//
//var f = new Test();
//console.log(f.constructor);
//console.log(f.toString());


//var toString = Object.prototype.toString;
//console.log(Object.toString.call(new Date));  
</script>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容