[英]Does Javascript have a BasicObject similar to Ruby?
In Ruby, in order to get the absolute cleanest inheritance chain, you can inherit from BasicObject
instead of Object
. 在Ruby中,为了获得绝对最干净的继承链,您可以继承
BasicObject
而不是Object
。 This way you don't have an object with methods you don't necessarily want (the methods that are part of Object.prototype
). 这样你就没有一个你不一定需要的方法的对象(属于
Object.prototype
的方法)。
Does JavaScript have a similar means of defining a basic object? JavaScript是否有类似的方法来定义基本对象?
function Person(name){
this.name = name
}
var mac = new Person('Mac')
delete mac.toString //does not work
delete mac.hasOwnProperty //does not work
Once you instantiate an object via a constructor function, it is not possible to delete
properties from the object if those properties are actually methods found on the prototype. 通过构造函数实例化对象后,如果这些属性实际上是在原型上找到的方法,则无法从对象中
delete
属性。
I don't want these methods on my object. 我不想在我的对象上使用这些方法。
You can create an object with no properties at all by calling Object.create(null)
. 您可以通过调用
Object.create(null)
来创建一个完全没有属性的对象。
You can then set that as a function's prototype
. 然后,您可以将其设置为函数的
prototype
。
(instead of the default prototype, which starts as Object.create(Object.prototype)
. (而不是默认原型,它以
Object.create(Object.prototype)
开头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.