Last active
August 29, 2015 14:22
-
-
Save slamorsi/88dabee07ad6ff67fb72 to your computer and use it in GitHub Desktop.
JS Bin// source http://jsbin.com/buwehi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
//in general, we see things as objects that have properties. we can model our programs this way too so that they’re easier for us to read and write and explain to others. | |
var Car = { | |
wheels: 4, | |
color: 'white', | |
transmission: 'auto' | |
}; | |
console.log(Car); | |
console.log(Car.color); | |
var heroCar = { | |
wheels: 4, | |
color: 'silver', | |
transmission: 'manual' | |
}; | |
var evilCar = { | |
wheels: 4, | |
color: 'red', | |
transmission: 'auto' | |
}; | |
//We'd like to share some of the common properties. This is where the concept of prototype comes in. | |
//every js object has a second js object associated with it. | |
//this second object is known as a prototype | |
console.log(Object.prototype); | |
//Object.prototype has no prototype | |
console.log(Object.getPrototypeOf(Object.prototype)); | |
//by default, all objects created have their prototype set to the root object's prototype | |
console.log(Object.getPrototypeOf(Car) == Object.prototype); | |
//Object.create creates a new object with the first argument being the prototype you want the new object to reference | |
var smartHeroCar = Object.create(Car); | |
//Since every object and every prototype (bar one) has a prototype, we can think of a succession of objects linked together to form a prototype chain. The end of the chain is always the root object’s prototype. | |
console.log(Object.getPrototypeOf(smartHeroCar) == Car); | |
//The way it works is whenever a property on an object is called it will start at the object and then go up through the prototype chain until it finds a match, or fail after the root Object prototype. This is how Javascript allows for runtime building and modification of objects; it has a plan for searching for what it needs. | |
console.log(smartHeroCar.color); | |
console.log(smartHeroCar.horsePower); | |
var smartEvilCar = Object.create(Car); | |
//prototype objects are shared | |
console.log(Object.getPrototypeOf(smartHeroCar) == Object.getPrototypeOf(smartEvilCar)); | |
Car.color = 'gold'; | |
console.log(smartHeroCar.color); | |
console.log(smartEvilCar.color); | |
//can still modify the property of the individual object | |
smartEvilCar.color = 'black'; | |
console.log(smartEvilCar.color); | |
console.log(smartHeroCar.color); | |
var bossEvilCar = Object.create(smartEvilCar); | |
console.log(Object.getPrototypeOf(bossEvilCar) == smartEvilCar); | |
console.log(Object.getPrototypeOf(smartEvilCar) == Car); | |
console.log(bossEvilCar.color); | |
</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//in general, we see things as objects that have properties. we can model our programs this way too so that they’re easier for us to read and write and explain to others. | |
var Car = { | |
wheels: 4, | |
color: 'white', | |
transmission: 'auto' | |
}; | |
console.log(Car); | |
console.log(Car.color); | |
//We'd like to share some of the common properties. This is where the concept of prototype comes in. | |
//every js object has a second js object associated with it. | |
//this second object is known as a prototype | |
console.log(Object.prototype); | |
//by default, all objects created have their prototype set to the root object's prototype | |
console.log(Object.getPrototypeOf(Car) == Object.prototype); | |
//Object.prototype has no prototype | |
console.log(Object.getPrototypeOf(Object.prototype) == null); | |
//Since every object and every prototype (bar one) has a prototype, we can think of a succession of objects linked together to form a prototype chain. The end of the chain is always the root object’s prototype. | |
//Object.create creates a new object with the first argument being the prototype you want the new object to reference | |
var smartHeroCar = Object.create(Car); | |
console.log(Object.getPrototypeOf(smartHeroCar) == Car); | |
//The way it works is whenever a property on an object is called it will start at the object and then go up through the prototype chain until it finds a match, or fail after the root Object prototype. This is how Javascript allows for runtime building and modification of objects; it has a plan for searching for what it needs. | |
console.log(smartHeroCar.color); | |
console.log(smartHeroCar.horsePower); | |
var smartEvilCar = Object.create(Car); | |
//prototype objects are shared | |
console.log(Object.getPrototypeOf(smartHeroCar) == Object.getPrototypeOf(smartEvilCar)); | |
Car.color = 'gold'; | |
console.log(smartHeroCar.color); | |
console.log(smartEvilCar.color); | |
//can still modify the property of the individual object | |
smartEvilCar.color = 'black'; | |
console.log(smartEvilCar.color); | |
console.log(smartHeroCar.color); | |
var bossEvilCar = Object.create(smartEvilCar); | |
console.log(Object.getPrototypeOf(bossEvilCar) == smartEvilCar); | |
console.log(Object.getPrototypeOf(smartEvilCar) == Car); | |
console.log(bossEvilCar.color); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment