Object.create( proto[, propertiesObject ] )
var human = {
name: 'Some human',
sayName: function()
{
console.log( 'My name is ' + this.name );
}
};
human.sayName();
var bob = Object.create( human );
bob.sayName();
bob.name = 'Bob';
bob.sayName();
human['class'] = 'Mammalia';
bob['class']; // 'Mammalia'
Запрос свойства: bob -> bob.prototype -> human.prototype -> Object.prototype -> undefined
.
.hasOwnProperty( prop )
— проверка наличия свойства непосредственно у объекта, не затрагивая цепочку прототипов.
bob.hasOwnProperty( 'name' ) // true
bob.hasOwnProperty( 'sayName' ) // false
bob.hasOwnProperty( 'class' ) // false
var key;
for ( key in bob )
{
if ( bob.hasOwnProperty( key ) )
{
console.log( key, bob[key] );
}
}
object instanceof constructor
— проверить, есть ли constructor в цепочке прототипов object.