Last active
August 29, 2015 13:56
-
-
Save zhiyelee/8912567 to your computer and use it in GitHub Desktop.
__proto__
This file contains hidden or 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
var animal = { eats: true } | |
var rabbit = { jumps: true } | |
rabbit.__proto__ = animal // inherit | |
console.log(rabbit.eats) // true | |
var animal = { eats: true } | |
rabbit = Object.create(animal); | |
// true | |
console.log(rabbit.eats); |
This file contains hidden or 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
function Animal() { | |
this.name = 'wangcai'; | |
} | |
function Dog() {} | |
Dog.prototype = new Animal(); | |
var d1 = new Dog(); | |
console.log(d1.name); | |
console.log(d1 instanceof Animal); | |
// change the prototype of Class | |
Dog.prototype = { | |
name: 'yingcai' | |
}; | |
// now instanceof ?? | |
console.log(d1 instanceof Dog); | |
console.log(d1 instanceof Animal); | |
console.log(Animal.prototype.__proto__ === Object.prototype); |
This file contains hidden or 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 name="description" content="instanceof cross iframe" /> | |
<meta charset="utf-8"> | |
<title>instanceof cross iframe</title> | |
</head> | |
<body> | |
<iframe id="fr" ></iframe> | |
<script> | |
var fr = document.getElementById('fr'); | |
var arr = []; | |
var ArrayFr = fr.contentWindow.Array; | |
var arr2 = new ArrayFr(); | |
console.log(Object.prototype.toString.call(arr)); | |
console.log(Object.prototype.toString.call(arr2)); | |
console.log(arr instanceof Array); | |
console.log(arr instanceof ArrayFr); | |
</script> | |
</body> | |
</html> |
This file contains hidden or 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
function Animal() { | |
this.eat = true; | |
} | |
var cat = new Animal(); | |
console.log(cat.eat); | |
Animal.prototype.jump = true; | |
console.log(cat.jump); | |
Animal.prototype = { | |
bark: true | |
}; | |
var dog = new Animal(); | |
console.log(cat.bark); | |
console.log(dog.bark); | |
cat.__proto__ = Animal.prototype; | |
console.log(cat.bark); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment