73 lines
2.1 KiB
HTML
73 lines
2.1 KiB
HTML
|
<!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.0">
|
||
|
<title>Document</title>
|
||
|
<script>
|
||
|
var val=999
|
||
|
var obj={name:"ming"} // 字面量创建
|
||
|
function Grand(){
|
||
|
this.name="root"
|
||
|
this.pow=2
|
||
|
this.say=function(){
|
||
|
console.log("我是根")
|
||
|
}
|
||
|
}
|
||
|
var grand1=new Grand()
|
||
|
console.log(grand1.__proto__) //grand1 obj 中有个 prototype, 对象里面只能用__proto__ 访问原型(左右两个横线)
|
||
|
console.log(Grand.prototype) //Grand 函数里面有个 prototype
|
||
|
// console.log(Grand.__proto__) error写法 函数里面只能用prototype属性访问原型
|
||
|
|
||
|
|
||
|
function f(){
|
||
|
this.name="father"
|
||
|
|
||
|
}
|
||
|
f.prototype=grand1 //原型指向的只能是实例对象 不能是函数
|
||
|
f.prototype.val="miku"
|
||
|
var fa=new f()
|
||
|
fa.say()
|
||
|
console.log(fa)
|
||
|
|
||
|
var me={}
|
||
|
me.__proto__=fa
|
||
|
console.log(me.name)
|
||
|
console.log(me.val)
|
||
|
//grand1 => fa =>me :原型链
|
||
|
|
||
|
// me.pow
|
||
|
// me 从me对象中查找pow属性的值
|
||
|
// |
|
||
|
// fa me对象中找不到属性时 就会去 上一级 也就是fa对象中找 Pow的值
|
||
|
// |
|
||
|
// grand1 fa 对象中也找不到这个属性的话 ,那么就再往上一层 找grand1中的 pow的值
|
||
|
|
||
|
// 如grand1中也找不到这个值呢 就结果上来说 结果是Undefined 而且原型链的尽头不是window
|
||
|
function A(sa){
|
||
|
this.sa = sa;
|
||
|
this.hello = function(){console.log("hello")}
|
||
|
}
|
||
|
function Aa(saa){
|
||
|
this.saa = saa;
|
||
|
|
||
|
}
|
||
|
function Aaa(saaa){
|
||
|
this.saaa = saaa;
|
||
|
|
||
|
}
|
||
|
var z = new A();
|
||
|
Aa.prototype = z;
|
||
|
var za = new Aa();
|
||
|
Aaa.prototype = za;
|
||
|
var zaa = new Aaa();
|
||
|
|
||
|
zaa.hello();
|
||
|
|
||
|
</script>
|
||
|
</head>
|
||
|
<body>
|
||
|
|
||
|
</body>
|
||
|
</html>
|