js中的原型与原型链


function Person(){}

1、prototype(有且仅有函数有该属性)

       1、 函数有prototype属性,该属性指向显示原型、原型对象

       2、 原型对象:拥有prototype属性的对象,在定义函数时自动创建

2、constructor

        原型对象的constructor属性,指向构造函数本身

3、__proto__(引用类型都有该属性)

       1、 原型对象有__proto__属性(隐式原型),该属性指向了Object的显示原型


        2、 Object也有__proto__属性,因为Object是顶级对象,所以这时指向null

        3、Object的构造函数

4、实例化对象

        构造函数的实例有__proto__属性,并且该属性指向原型对象

        也就是说对象的隐式原型指向构造函数的显式原型,两者等价,所以有

const p = new Person()
console.log(p);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些 JS 高级原型原型链的练习题,希望对您有所帮助。 1. 创建一个构造函数 Person,它有属性 name 和 age。创建一个对象 john,它的 name 是 "John",age 是 30。使用 Person 的原型对象添加一个方法 sayHello,它会在控制台输出 "Hello, my name is {name}!"。 ```javascript function Person(name, age) { this.name = name; this.age = age; } Person.prototype.sayHello = function() { console.log(`Hello, my name is ${this.name}!`); }; const john = new Person("John", 30); john.sayHello(); ``` 2. 创建一个构造函数 Animal,它有属性 name 和 type。创建一个对象 cat,它的 name 是 "Whiskers",type 是 "Cat"。使用 Object.create() 方法创建一个对象 dog,它的 name 是 "Fido",type 是 "Dog"。使用 Animal 的原型对象添加一个方法 sayType,它会在控制台输出 "{name} is a {type}."。 ```javascript function Animal(name, type) { this.name = name; this.type = type; } Animal.prototype.sayType = function() { console.log(`${this.name} is a ${this.type}.`); }; const cat = new Animal("Whiskers", "Cat"); const dog = Object.create(Animal.prototype, { name: { value: "Fido", writable: true, enumerable: true, configurable: true }, type: { value: "Dog", writable: true, enumerable: true, configurable: true } }); cat.sayType(); // Whiskers is a Cat. dog.sayType(); // Fido is a Dog. ``` 3. 创建一个构造函数 Shape,它有属性 color 和 name。创建一个对象 circle,它的 color 是 "red",name 是 "Circle"。创建一个对象 square,它的 color 是 "green",name 是 "Square"。使用 Shape 的原型对象添加一个方法 describe,它会在控制台输出 "This is a {color} {name}."。 ```javascript function Shape(color, name) { this.color = color; this.name = name; } Shape.prototype.describe = function() { console.log(`This is a ${this.color} ${this.name}.`); }; const circle = new Shape("red", "Circle"); const square = new Shape("green", "Square"); circle.describe(); // This is a red Circle. square.describe(); // This is a green Square. ``` 希望这些练习题能够帮助您巩固 JS 高级原型原型链的知识。如果您有任何问题或需要进一步的帮助,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值