Skip to content

Instantly share code, notes, and snippets.

@koyta
Created May 25, 2017 10:49
Show Gist options
  • Save koyta/56c1137a2baa5fbeb8d39664b10f2e4f to your computer and use it in GitHub Desktop.
Save koyta/56c1137a2baa5fbeb8d39664b10f2e4f to your computer and use it in GitHub Desktop.
Прототипное наследование

Прототипное наследование

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment