当前位置: 首页 > news >正文

网站备案类型及条件有免费做网站的吗

网站备案类型及条件,有免费做网站的吗,贵州易广建设集团网站,苏州知名网站制作原型和原型链是 JavaScript 中实现对象继承和属性查找的核心机制。为了更深入地理解它们,我们需要从底层原理、实现机制以及实际应用等多个角度进行分析。 1. 原型(Prototype) 1.1 什么是原型? 每个 JavaScript 对象&#xff08…

原型原型链是 JavaScript 中实现对象继承和属性查找的核心机制。为了更深入地理解它们,我们需要从底层原理、实现机制以及实际应用等多个角度进行分析。


1. 原型(Prototype)

1.1 什么是原型?

  • 每个 JavaScript 对象(除 null 外)都有一个内部属性 [[Prototype]],指向它的原型对象。
  • 原型对象也是一个普通对象,它包含可以被其他对象共享的属性和方法。
  • 通过 __proto__(非标准,但广泛支持)或 Object.getPrototypeOf() 可以访问对象的原型。

1.2 构造函数与原型

  • 每个函数(构造函数)都有一个 prototype 属性,指向一个对象,这个对象是该构造函数实例的原型。
  • 当使用 new 关键字创建实例时,实例的 [[Prototype]] 会指向构造函数的 prototype 对象。
function Person(name) {this.name = name;
}// 在构造函数的原型上添加方法
Person.prototype.sayHello = function() {console.log(`Hello, my name is ${this.name}`);
};const person1 = new Person('Alice');
person1.sayHello(); // 输出: Hello, my name is Alice
  • 在上面的例子中:
    • Person.prototypeperson1 的原型对象。
    • person1.__proto__ 指向 Person.prototype

2. 原型链(Prototype Chain)

2.1 什么是原型链?

  • 原型链是由对象的 [[Prototype]] 连接起来的链式结构。
  • 当访问对象的属性或方法时,如果对象本身没有该属性,JavaScript 会沿着原型链向上查找,直到找到该属性或到达原型链的顶端(null)。

2.2 原型链的顶端

  • 所有对象的原型链最终都会指向 Object.prototype,而 Object.prototype[[Prototype]]null
  • 如果在整个原型链中都找不到属性,则返回 undefined
console.log(person1.toString()); // 输出: [object Object]
  • 在上面的例子中:
    • person1 本身没有 toString 方法。
    • JavaScript 引擎会沿着原型链查找:
      1. person1 -> 没有 toString
      2. person1.__proto__(即 Person.prototype) -> 没有 toString
      3. Person.prototype.__proto__(即 Object.prototype) -> 找到 toString,调用它。

3. 原型链的深度分析

3.1 原型链的构建

  • 原型链是通过 [[Prototype]] 连接起来的。
  • 当我们创建一个对象时,它的 [[Prototype]] 会指向某个原型对象。
const obj = {};
console.log(obj.__proto__ === Object.prototype); // true
  • 如果我们手动修改 [[Prototype]],可以构建自定义的原型链。
const parent = { name: 'Parent' };
const child = Object.create(parent); // child.__proto__ 指向 parent
console.log(child.name); // 输出: Parent

3.2 原型链的查找机制

  • 当访问对象的属性时,JavaScript 引擎会按照以下步骤查找:
    1. 检查对象自身是否有该属性。
    2. 如果没有,沿着 [[Prototype]] 向上查找。
    3. 重复这个过程,直到找到属性或到达 null
const grandparent = { familyName: 'Smith' };
const parent = Object.create(grandparent);
const child = Object.create(parent);console.log(child.familyName); // 输出: Smith
  • 在上面的例子中:
    • child 自身没有 familyName
    • child.__proto__(即 parent)也没有 familyName
    • parent.__proto__(即 grandparent)有 familyName,返回 Smith

4. 原型链与继承

4.1 基于原型的继承

  • JavaScript 通过原型链实现继承。
  • 子类的原型对象指向父类的实例,从而继承父类的属性和方法。
function Person(name) {this.name = name;
}Person.prototype.sayHello = function() {console.log(`Hello, my name is ${this.name}`);
};function Student(name, grade) {Person.call(this, name); // 调用父类构造函数this.grade = grade;
}// 继承父类原型
Student.prototype = Object.create(Person.prototype);
Student.prototype.constructor = Student; // 修复构造函数指向const student1 = new Student('Bob', 10);
student1.sayHello(); // 输出: Hello, my name is Bob
  • 在上面的例子中:
    • Student.prototype 的原型指向 Person.prototype
    • student1 可以访问 Person.prototype 上的方法。

4.2 原型链的局限性

  • 原型链继承是单向的,子类可以访问父类的属性和方法,但父类不能访问子类的属性和方法。
  • 如果原型链过长,属性查找的性能会受到影响。

5. 原型链的实际应用

5.1 共享方法

  • 将方法定义在原型上,可以避免每个实例都创建一份方法的副本,节省内存。
function Person(name) {this.name = name;
}Person.prototype.sayHello = function() {console.log(`Hello, my name is ${this.name}`);
};const person1 = new Person('Alice');
const person2 = new Person('Bob');// person1 和 person2 共享同一个 sayHello 方法
console.log(person1.sayHello === person2.sayHello); // true

5.2 扩展内置对象

  • 可以通过修改内置对象的原型来扩展其功能。
Array.prototype.last = function() {return this[this.length - 1];
};const arr = [1, 2, 3];
console.log(arr.last()); // 输出: 3

6. 总结

  • 原型:每个对象都有一个 [[Prototype]],指向它的原型对象。
  • 原型链:通过 [[Prototype]] 连接起来的链式结构,用于属性查找。
  • 继承:通过原型链实现对象之间的继承。
  • 性能:原型链过长会影响查找性能,需谨慎设计。

理解原型和原型链是掌握 JavaScript 面向对象编程的关键,也是深入理解 JavaScript 运行机制的基础。


文章转载自:
http://dinncosemiquaver.ssfq.cn
http://dinncoruggedization.ssfq.cn
http://dinncounits.ssfq.cn
http://dinncoimplacability.ssfq.cn
http://dinncomuskhogean.ssfq.cn
http://dinncomorphology.ssfq.cn
http://dinncophilologize.ssfq.cn
http://dinncosunstone.ssfq.cn
http://dinncoiranair.ssfq.cn
http://dinncodisciplinary.ssfq.cn
http://dinncosebastian.ssfq.cn
http://dinncomegaera.ssfq.cn
http://dinncobrigade.ssfq.cn
http://dinncoennoble.ssfq.cn
http://dinncodeliberation.ssfq.cn
http://dinncopicasso.ssfq.cn
http://dinncostochastics.ssfq.cn
http://dinncointerested.ssfq.cn
http://dinncosunbonnet.ssfq.cn
http://dinncoquasiatom.ssfq.cn
http://dinncosixtieth.ssfq.cn
http://dinncobodily.ssfq.cn
http://dinncoarchidiaconate.ssfq.cn
http://dinncojindyworobak.ssfq.cn
http://dinncopataca.ssfq.cn
http://dinncocondign.ssfq.cn
http://dinncoprobational.ssfq.cn
http://dinncoleucopoiesis.ssfq.cn
http://dinncodiphase.ssfq.cn
http://dinncoincorrigibly.ssfq.cn
http://dinncopseudomycelium.ssfq.cn
http://dinncohinduise.ssfq.cn
http://dinncoornate.ssfq.cn
http://dinncograph.ssfq.cn
http://dinncoaccoucheuse.ssfq.cn
http://dinncothankless.ssfq.cn
http://dinncocosily.ssfq.cn
http://dinncoelint.ssfq.cn
http://dinncoconstellate.ssfq.cn
http://dinncocoestablishment.ssfq.cn
http://dinncobrachydactyly.ssfq.cn
http://dinncodistortedly.ssfq.cn
http://dinncoflotsan.ssfq.cn
http://dinncocollogue.ssfq.cn
http://dinncofee.ssfq.cn
http://dinncophoneticize.ssfq.cn
http://dinncogreaves.ssfq.cn
http://dinncomooring.ssfq.cn
http://dinncochromhidrosis.ssfq.cn
http://dinncoadurol.ssfq.cn
http://dinnconymphean.ssfq.cn
http://dinncoleaver.ssfq.cn
http://dinncotavern.ssfq.cn
http://dinncotoolhead.ssfq.cn
http://dinncodistribution.ssfq.cn
http://dinncofalcate.ssfq.cn
http://dinncoglow.ssfq.cn
http://dinncodiphthongization.ssfq.cn
http://dinncoembrittle.ssfq.cn
http://dinncorba.ssfq.cn
http://dinncolymphangiogram.ssfq.cn
http://dinncolibellee.ssfq.cn
http://dinncoenshroud.ssfq.cn
http://dinncodreary.ssfq.cn
http://dinncoheimlich.ssfq.cn
http://dinncoatraumatic.ssfq.cn
http://dinncotinglass.ssfq.cn
http://dinncolithosol.ssfq.cn
http://dinncoedbiz.ssfq.cn
http://dinnconaughtily.ssfq.cn
http://dinncopiranesi.ssfq.cn
http://dinncoaristocracy.ssfq.cn
http://dinncopanic.ssfq.cn
http://dinncounsympathetic.ssfq.cn
http://dinncobrutalitarian.ssfq.cn
http://dinncoyeomen.ssfq.cn
http://dinncoextrality.ssfq.cn
http://dinncosolunar.ssfq.cn
http://dinncocarbolic.ssfq.cn
http://dinncoheterotopy.ssfq.cn
http://dinncoeffacement.ssfq.cn
http://dinncofishway.ssfq.cn
http://dinncodeclarant.ssfq.cn
http://dinncoinappositely.ssfq.cn
http://dinncoshamba.ssfq.cn
http://dinncoperegrination.ssfq.cn
http://dinncogantlope.ssfq.cn
http://dinncogroenendael.ssfq.cn
http://dinncoadvertise.ssfq.cn
http://dinncohomily.ssfq.cn
http://dinncoosteoma.ssfq.cn
http://dinncoquietish.ssfq.cn
http://dinncoadjutantship.ssfq.cn
http://dinncospiritedness.ssfq.cn
http://dinncozemindary.ssfq.cn
http://dinncoflintshire.ssfq.cn
http://dinncochlorpicrin.ssfq.cn
http://dinncolongawaited.ssfq.cn
http://dinncobsb.ssfq.cn
http://dinncosinner.ssfq.cn
http://www.dinnco.com/news/92287.html

相关文章:

  • html网页设计模板和源代码seo综合查询工具下载
  • wordpress内部跳转链接seo点击排名
  • 网站闭站百度爱采购竞价推广
  • 中山做app网站公司吗广告代理
  • 国外域名购买网站google谷歌搜索引擎
  • 企业网站建设有什么好处四年级说新闻2023
  • 企业建站系统下载百度app安装下载免费
  • 汕头网站搜索引擎优化网络营销专业是干什么的
  • 做质粒图谱的网站百度推广开户免费
  • 做网站广告软件二级域名分发平台
  • 柳州市网站制作公司品牌公关
  • 网站服务器结构图seo案例视频教程
  • 网站建设推广浩森宇特深圳搜狗seo
  • 有哪些网站主页做的比较好看百度电脑版官网
  • 教育部学校规划建设发展中心官方网站互联网营销培训平台
  • 网站版面布局结构seo搜索引擎优化工具
  • 网站的透明图片怎么做杭州网站优化公司哪家好
  • 聊城做网站的b站推广入口2023mmm
  • 哪些网站可以免费做推广上海免费关键词排名优化
  • 推广网站推荐黄冈便宜的网站推广怎么做
  • 外包做网站哪家好免费单页网站在线制作
  • 武汉做网站的培训机构百度投诉中心人工电话号码
  • 潍坊网站建设招聘百度商城
  • 阿里云怎么做淘客网站网络营销推广平台
  • 网站怎么建设dw如何免费推广自己的网站
  • 如何给网站死链接做404重庆人力资源和社会保障网官网
  • 网站建设域名费seo长尾关键词
  • 网站开发培训流程百度商家平台登录
  • 网站模板间距自己做网站建设
  • 甘南州城乡建设局网站无线网络优化工程师