简体   繁体   中英

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 . This way you don't have an object with methods you don't necessarily want (the methods that are part of Object.prototype ).

Does JavaScript have a similar means of defining a basic object?

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.

I don't want these methods on my object.

You can create an object with no properties at all by calling Object.create(null) .

You can then set that as a function's prototype .
(instead of the default prototype, which starts as Object.create(Object.prototype) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM