Skip to content

Instantly share code, notes, and snippets.

@zhiyelee
Last active August 29, 2015 13:56
Show Gist options
  • Save zhiyelee/8912567 to your computer and use it in GitHub Desktop.
Save zhiyelee/8912567 to your computer and use it in GitHub Desktop.
__proto__
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);

proto

Object.create

getPrototypeOf

instanceof

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);
<!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>
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