原型链——1

本文通过两个实例详细解析了JavaScript中原型链的工作原理。首先定义了一个动物对象,包含吃的方法及颜色属性;接着创建狗和猫对象,它们继承自动物对象,并能够调用其方法。进一步介绍了如何使用构造函数创建对象,并通过原型属性为构造函数添加方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于原型链的理解

这里有两个例子看到后让我觉得一目了然

  var anmial = {
    name: "anmial",
  eat:function () {
    console.log(this.name + " is eating.");
  }
  }
  anmial.eat();//anmial is eating.

  anmial.color = "black";

  var dog = {
    name: "dog",
  __proto__:anmial    //原型指向 anmial
  }
  var cat = {
    name:"cat",
    __proto__:anmial
  }
  dog.eat();    //dog is eating.
  cat.eat();    //cat is eating
Created with Raphaël 2.1.2 dog dog anmial anmial cat cat Object.protptype Object.protptype __proto__ __proto__ __proto__
  function Student(name){
    this.name=name;
  }
  Student.prototype={
    sayHello:function () {
      console.log("Hi,I'm " + this.name);
    }
  }
  var andy=new Student("andy");
Created with Raphaël 2.1.2 andy andy Student.proptotype Student.proptotype Student(func) Student(func) Object.proptotype Object.proptotype __proto__ proptotype __proto__
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值