Last active
December 28, 2015 15:49
-
-
Save jocoonopa/7524250 to your computer and use it in GitHub Desktop.
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 lang="zh-tw"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>js prototype 和__proto__簡單demo</title> | |
</head> | |
<body> | |
<script> | |
var Person = function () { | |
this.name = '阿良良木曆'; | |
this.gender = '男'; | |
this.race = '吸血鬼'; | |
this.group = '後宮管理員'; | |
}; | |
Person.prototype.getName = function () { | |
return this.name + '阿良良木曆'; | |
}; | |
Person.prototype.getRace = function () { | |
return this.race + '吸血鬼'; | |
} | |
Person.prototype.getGender = function () { | |
return this.gender + '男'; | |
}; | |
Person.prototype.getGroup = function () { | |
return this.group + '後宮管理員'; | |
}; | |
var Mui = function () { | |
this.name = '戰場原'; | |
this.race = '人類'; | |
this.gender = '傲嬌'; | |
}; | |
Mui.prototype = new Person(); | |
Mui.prototype.name = '千石撫子'; | |
Mui.prototype.race = '蛇'; | |
Mui.prototype.gender = '萌'; | |
Mui.prototype.group = '成員'; | |
Mui.prototype.getGenders = function () { | |
return this.gender; | |
}; | |
Mui.prototype.getRace = function () { | |
return this.race; | |
} | |
Mui.prototype.getName = function () { | |
return this.name; | |
} | |
Mui.prototype.getInfo = function () { | |
return '資料'; | |
} | |
var Girl = new Mui(); | |
console.log( Girl.getName() ); // 往上找到Mui.prototype.getName() :output->戰場原 | |
console.log( Girl.__proto__.getName() ); // :output->千石撫子 | |
console.log( Girl.__proto__.__proto__.getName() ); // Person的方法 :output->undefined阿良良木曆 | |
console.log( Girl.__proto__.__proto__.getName.apply( Girl ) ); // Person的方法, 丟進物件Girl 因此 this 變成Girl :output->戰場原阿良良木曆 | |
// 同上 | |
console.log( Girl.getRace() ); // :output->人類 | |
console.log( Girl.__proto__.getRace() ); // :output->蛇 | |
console.log( Girl.__proto__.__proto__.getRace() ); // :output->undefined吸血鬼 | |
console.log( Girl.__proto__.__proto__.getRace.apply( Girl ) ); // :output->人類吸血鬼 | |
// 同上 | |
console.log( Girl.getGender() ); // :output->傲嬌男 | |
console.log( Girl.__proto__.getGender() ); // :output->萌男 | |
console.log( Girl.__proto__.__proto__.getGender() ); // :output->undefined男 | |
console.log( Girl.__proto__.__proto__.getGender.apply( Girl ) ); // :output->傲嬌男 | |
console.log( Girl.getGroup() ); // scope特性會去往上找 getGroup方法 -> | |
console.log( Girl.__proto__.getGroup() ); // scope特性會去往上找 getGroup方法 -> | |
console.log( Girl.__proto__.__proto__.getGroup.apply( Girl ) ); // 真正呼叫到的方法 -> | |
console.log( Girl.getInfo() ); // -> 資料 | |
console.log( Girl.__proto__.getInfo() ); // -> 資料 | |
console.log( Girl.__proto__.proto__.getInfo.apply( Girl ) ); // 錯誤, 因為Person無此方法 , 可得知scope特性是往上找不會往下找 | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment