博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
object.create_Object.create(空)
阅读量:2515 次
发布时间:2019-05-11

本文共 1759 字,大约阅读时间需要 5 分钟。

object.create

One of the funnest parts of JavaScript, or any programming language really, is that there are loads of tiny tricks and quirks that make the language that much more interesting.  I recently learned a nice fact about Object.create:  using null as the only argument to create an ultra-vanilla dictionary!

JavaScript或实际上是任何编程语言中最有趣的部分之一是,大量的小技巧和怪癖使该语言变得更加有趣。 我最近了解到一个关于Object.create的好事实:使用null作为创建超香草词典的唯一参数!

Object.create has been an awesome utility for prototype creation.  While that's nice, objects created with Object.create have __proto__ and inherited Object properties which can be manipulated.  What if you simply want a dictionary not prone to manipulation from the outside?  You can have that with Object.create(null):

Object.create是用于原型创建的强大工具。 很好,但是使用Object.create创建的对象具有__proto__和可以操纵的继承的Object属性。 如果您只是希望字典不易于受到外界的操纵该怎么办? 您可以使用Object.create(null)

let dict = Object.create(null);// dict.__proto__ === "undefined"// No object properties exist until you add them

Since there's no prototype your Object can't be manipulated from the outside -- it remains as vanilla of a dictionary as possible!  Compare that to Object.create({}):

由于没有原型,因此无法从外部操纵您的Object,它仍然尽可能地像字典一样! 将其与Object.create({})进行比较:

let obj = Object.create({});// obj.__proto__ === {}// obj.hasOwnProperty === functionObject.prototype.someFunction = () => {};// obj.someFunction === () => {};// dict.someFunction === undefined

Passing Object.create an empty object allows for properties to be added via Object.prototype.customPropName, something you may not always want.

传递Object.create一个空对象允许通过Object.prototype.customPropName添加属性,而您可能并不总是希望这样。

I hadn't known of this trick until recently but will be using it quite a bit going forward!

直到最近我才知道这个技巧,但是以后会用到很多!

翻译自:

object.create

转载地址:http://qyvwd.baihongyu.com/

你可能感兴趣的文章
Urozero Autumn 2016. UKIEPC 2016
查看>>
ActiveReport使用心得(四):WebForm下的显示类型及数据的导出
查看>>
《精通CSS:高级Web标准解决方案》学习笔记之二:可视化格式模型
查看>>
Flash调用Lua脚本: 五
查看>>
一次oracle大量数据删除经历
查看>>
npm重要知识点梳理
查看>>
Windows 7安装Service Pack 1失败问题
查看>>
How To Move Or Rebuild A Lob Partition
查看>>
请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理解决方案
查看>>
3-STM32物联网开发WIFI+GPRS基础篇(开发板测试-GPRS)
查看>>
CSS教程:彻底掌握Z-index属性
查看>>
C++中值类型形参、指针类型形参和引用形参的探讨
查看>>
css3:bacground-size
查看>>
双系统开机引导菜单修复方法 进win7无须重启|metro引导|双系统菜单名字修改
查看>>
apache整合tomcat中的一些注意事项
查看>>
VC++使用Pro*CC++
查看>>
前端角度出发做好SEO需要考虑什么?
查看>>
laravel扩展图片处理Intervention Image
查看>>
【PDF】HTML中嵌入pdf的简单方法
查看>>
java集合系列——Map介绍(七)
查看>>