<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA_Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
<link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
<script>
window.onload = function () {
function Star3(name,age) {
this.name = name;
this.age = age;
}
Star3.prototype.sing = function () {
console.log("唱歌!")
};
var lyj = new Star3('lyj',24);
//1. 只要是对象就有 __proto__ 原型,指向原型对象
console.log(Star3.prototype);
console.log(Star3.prototype.__proto__ === Object.prototype);//true
//2. 我们Star原型对象里面的__proto__原型指向的是 Object。prototype
console.log(Object.prototype.__proto__);//null
//查找机制
function Star4(name,age) {
this.name = name;
this.age = age;
}
var mh = new Star4('mh',25);
mh.sex = '男';
console.log(mh.sex);//男
//如果 mh 没有 sex 这个对象成员,mh就会根据 __proto__向上寻找 Star4
//有没有这个对象成员, 如下
var bms = new Star4('bms',26);
Star4.prototype.sex = '男';
console.log(bms.sex);//男
//同理如果Star4也没有,就会继续向上寻找(寻找Object.prototype.sex)
//查到 null 时,会输出 undefined
// JavaScript的成员查找机制(规则)
// 1当访问一个对象的属性(包括方法)时,首先倭找这个对象自身有没有该属性。
// 2如果没有就查找它的原型(也就是_proto_指向的prototype原型对象)。
// 3如果还没有就查找原型对象的原型(Object的原型对象)。
// 4依此类推一直找到Object为止( null )。
// 5如果 bms 、Star4、Object 都有 sex ,会优先输出 bms.sex(可以说是就近原则吧)
}
</script>
</head>
<body>
</body>
</html>