Ruby's Louvre

每天学习一点点算法

导航

创建纯空Object

ie9和其它浏览器可以用Object.create(null)纯空对象,没有任何成员的,IE的旧版本可以使用以下代码模拟,但需要维持doc的引用

//IronJS-上官荔枝  
NullObject = (function(){
  var doc = new ActiveXObject('htmlfile')
  doc.write('<script><\/script>')
  doc.close()
  var Obj = doc.parentWindow.Object
  if(!Obj || Obj === Object) return
  var name, names =
    [ 'constructor', 'hasOwnProperty', 'isPrototypeOf'
    , 'propertyIsEnumerable', 'toLocaleString', 'toString', 'valueOf']
  while(name = names.pop()) if(!delete Obj.prototype[name]) return
  return Obj
}())

测试代码:

({}).toString

Object.create(null).toString

posted on 2011-08-03 09:39  司徒正美  阅读(1524)  评论(2编辑  收藏  举报