
按照原型链来查找的:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
function Star(uname, age) {
this.uname = uname;
this.age = age;
}
Star.prototype.sing = function() {
console.log('我会唱歌');
};
var ldh = new Star('刘德华', 18);
ldh.sex='男';
console.log(ldh.sex);
</script>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
function Star(uname, age) {
this.uname = uname;
this.age = age;
}
Star.prototype.sing = function() {
console.log('我会唱歌');
};

本文探讨JavaScript中成员查找的规则,通过原型链进行属性查找。当对象实例包含属性时,不会继续沿原型链向上查找。即使实例中不存在toString方法,也能调用到Object.prototype中的toString方法,这是原型链查找的原理。
最低0.47元/天 解锁文章
9018

被折叠的 条评论
为什么被折叠?



