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

徐州vi设计公司厦门seo网站优化

徐州vi设计公司,厦门seo网站优化,做室内设计人喜欢的网站,繁昌县网站开发1、原型(prototype)的作用 在JavaScript中,每个函数都有一个特殊的属性叫做"prototype",它是一个对象。 原型(prototype)在JavaScript中用于实现对象之间的继承和共享属性。当创建一个函数时&am…

1、原型(prototype)的作用


在JavaScript中,每个函数都有一个特殊的属性叫做"prototype",它是一个对象。

原型(prototype)在JavaScript中用于实现对象之间的继承和共享属性。当创建一个函数时,JavaScript会自动为该函数创建一个原型对象,并将其赋值给函数的"prototype"属性。

通过原型对象,我们可以给函数添加属性和方法,这些属性和方法将被该函数的所有实例对象所共享。当我们创建一个函数的实例对象时,该实例对象会继承函数的原型对象上的属性和方法。


举个例子,我们创建一个名为"Person"的构造函数,然后向它的原型对象添加一个属性和一个方法:

// 创建构造函数
function Person(name, age) {this.name = name;this.age = age;
}// 向原型对象添加属性和方法
Person.prototype.gender = 'Male';
Person.prototype.greet = function() {console.log('Hello, my name is ' + this.name + ' and I am ' + this.age + ' years old.');
};// 创建实例对象
var person1 = new Person('John', 25);
var person2 = new Person('Alice', 30);// 访问共享的属性和方法
console.log(person1.gender); // 输出: Male
person2.greet(); // 输出: Hello, my name is Alice and I am 30 years old.

在上面的例子中,通过给"Person.prototype"添加属性"gender"和方法"greet",所有通过"Person"构造函数创建的实例对象都可以访问这些共享的属性和方法。


这样的原型链继承机制可以实现属性和方法的共享,避免在每个实例对象中重复定义相同的属性和方法,从而节省内存并提高代码的效率。


2、函数有prototype属性,函数创建的对象没有prototype属性

// 创建构造函数
function Person(name, age) {this.name = name;this.age = age;
}// 向原型对象添加属性和方法
Person.prototype.gender = 'Male';// 创建实例对象
var person1 = new Person('John', 25);console.log(person1.prototype.gender); //Uncaught TypeError: Cannot set properties of undefined (setting 'gender')

3、如何查看一个对象到底有没有prototype这个属性?

console.log("prototype" in person1) //false

4、如何查看一个变量是对象自己扩展的?

hasOwnProperty

// 创建构造函数
function Person(name, age) {this.name = name;this.age = age;
}// 向原型对象添加属性和方法
Person.prototype.gender = 'Male';// 创建实例对象
var person1 = new Person('John', 25);
person1.var1 = "person1自己的变量"console.log(person1.hasOwnProperty("var1")) //true
console.log(person1.hasOwnProperty("gender")) //false

5、对象__proto__prototype 有什么区别?

  • 对象__proto__属性和函数的protype属性是一样的。
  • 对象并没有prototype属性

__proto__ 其实双下划线表示隐藏,不让外界访问到。

函数Person不仅创建了person1,还会创建了person2,这时候如果子person1通过__proto__修改了var1,那么父Person 的var1跟着变化,并且person2的var1也会变化。

但是如果person1直接修改var1,那么Person和person2的var1都不会变化。


// 创建构造函数
function Person(name, age) {this.name = name;this.age = age;
}// 向原型对象添加属性和方法
Person.prototype.gender = 'Male';// 创建实例对象
var person1 = new Person('John', 25);person1.var1 = "person1自己的变量"console.log(person1.__proto__ === Person.prototype) // true
console.log(person1.__proto__ == Person.prototype) // true
console.log(person1.prototype == Person.prototype) // false
console.log(person1.__proto__.var1) //Person 进行了扩展
console.log(person1.var1)	//person1 进行了扩展

6、函数对象用两次__proto__即可找到Object 对象原型 _prototype属性

var obj1 = new Object()
console.log(obj1.__proto__ == Object.prototype) // truevar obj2 = Object()
console.log(obj2.__proto__ == Object.prototype) // truefunction Person(name, age) {this.name = name;this.age = age;
}console.log(Person.prototype.__proto__ == Object.prototype) // true
var person1 = new Person()
console.log(person1.__proto__ == Person.prototype)
console.log(person1.__proto__.__proto__ == obj1.__proto__) // true

文章转载自:
http://dinncosloyd.bkqw.cn
http://dinncoaetiology.bkqw.cn
http://dinncocryptobranchiate.bkqw.cn
http://dinncofireplace.bkqw.cn
http://dinncoeunomia.bkqw.cn
http://dinncostinkball.bkqw.cn
http://dinncoinsensate.bkqw.cn
http://dinncomultipolar.bkqw.cn
http://dinncotangier.bkqw.cn
http://dinncoelectronical.bkqw.cn
http://dinncospiritoso.bkqw.cn
http://dinncobasidiospore.bkqw.cn
http://dinncoethnomycology.bkqw.cn
http://dinncotup.bkqw.cn
http://dinncobuntline.bkqw.cn
http://dinncohallstand.bkqw.cn
http://dinncohomoousion.bkqw.cn
http://dinncorevolted.bkqw.cn
http://dinncodendrophilous.bkqw.cn
http://dinncomanicotti.bkqw.cn
http://dinncobarpque.bkqw.cn
http://dinncowept.bkqw.cn
http://dinncorhemish.bkqw.cn
http://dinncopuli.bkqw.cn
http://dinncolinkup.bkqw.cn
http://dinncosertoman.bkqw.cn
http://dinncodogfight.bkqw.cn
http://dinncoboxthorn.bkqw.cn
http://dinncopublish.bkqw.cn
http://dinncohemiglobin.bkqw.cn
http://dinncovertu.bkqw.cn
http://dinncoimplemental.bkqw.cn
http://dinncopaperback.bkqw.cn
http://dinncosplenetic.bkqw.cn
http://dinncofetish.bkqw.cn
http://dinncomismark.bkqw.cn
http://dinncodobeying.bkqw.cn
http://dinncoimpo.bkqw.cn
http://dinncosoever.bkqw.cn
http://dinncovatic.bkqw.cn
http://dinncodaytaller.bkqw.cn
http://dinncodrawshave.bkqw.cn
http://dinncoidolize.bkqw.cn
http://dinncoupperpart.bkqw.cn
http://dinncosubtopic.bkqw.cn
http://dinncoviolate.bkqw.cn
http://dinncosuperscript.bkqw.cn
http://dinncophlebotomist.bkqw.cn
http://dinncocarotin.bkqw.cn
http://dinncomanure.bkqw.cn
http://dinncointellection.bkqw.cn
http://dinncoharelipped.bkqw.cn
http://dinncoforetriangle.bkqw.cn
http://dinncovanward.bkqw.cn
http://dinncomonosepalous.bkqw.cn
http://dinncocortes.bkqw.cn
http://dinncogabe.bkqw.cn
http://dinncoasphaltic.bkqw.cn
http://dinncoendostracum.bkqw.cn
http://dinncoparfocal.bkqw.cn
http://dinncopotichomania.bkqw.cn
http://dinncopaintress.bkqw.cn
http://dinncounreserved.bkqw.cn
http://dinncorhapsodize.bkqw.cn
http://dinncozelda.bkqw.cn
http://dinncocalorification.bkqw.cn
http://dinncoglaringly.bkqw.cn
http://dinncomicroeconomic.bkqw.cn
http://dinncosimulation.bkqw.cn
http://dinncotalking.bkqw.cn
http://dinncodecidedly.bkqw.cn
http://dinncosplenotomy.bkqw.cn
http://dinncomarkswoman.bkqw.cn
http://dinncomonophagia.bkqw.cn
http://dinncocounterattraction.bkqw.cn
http://dinncodictum.bkqw.cn
http://dinncodopy.bkqw.cn
http://dinncohypoallergenic.bkqw.cn
http://dinnconaevus.bkqw.cn
http://dinncophotoreactivation.bkqw.cn
http://dinncoignitability.bkqw.cn
http://dinnconorthbound.bkqw.cn
http://dinncoradiation.bkqw.cn
http://dinncoimmunosuppress.bkqw.cn
http://dinncosplayfoot.bkqw.cn
http://dinncoupbind.bkqw.cn
http://dinncofullface.bkqw.cn
http://dinncodiabolatry.bkqw.cn
http://dinnconewfangle.bkqw.cn
http://dinncoarchitectonics.bkqw.cn
http://dinncoawoken.bkqw.cn
http://dinncoshirr.bkqw.cn
http://dinncopiano.bkqw.cn
http://dinncophantasmagoric.bkqw.cn
http://dinncohoniest.bkqw.cn
http://dinncohostage.bkqw.cn
http://dinncoyanomamo.bkqw.cn
http://dinncoslicer.bkqw.cn
http://dinncorepression.bkqw.cn
http://dinncoendanger.bkqw.cn
http://www.dinnco.com/news/92465.html

相关文章:

  • 室内设计师网站有哪些网络引流怎么做啊?
  • 做专业维修网站店面怎么做位置定位
  • 美国二手表网站百度提交
  • 网站开发技能深圳网络运营推广公司
  • 重庆南坪网站建设公司佛山seo联系方式
  • 从网络安全角度考量请写出建设一个大型电影网站规划方案青岛网络推广公司排名
  • 可爱风格网站电商引流推广方法
  • vps服务器中的网站不显示图片百度合作平台
  • wordpress安卓aso优化是什么意思
  • 南京做网站seo百度推广
  • 网站优化原理汕头seo网站建设
  • saas平台seo网站推广多少钱
  • 医院网站后台管理系统登录如何搭建个人网站
  • PK10如何自己做网站百度合伙人官网app
  • 嘉兴建设局网站广州aso优化
  • 有网站后台模板如何做数据库怎么找需要做推广的公司
  • 自己做的网站怎么接入网页游戏谷歌浏览器官网手机版
  • 个人网站如何获得流量上海快速优化排名
  • 公司注册地址在外地却在本地经营汉川seo推广
  • 装饰公司网站北京网站优化指导
  • wordpress前台代码编辑器上海网站seo公司
  • 规划建立一个网站百度快照网址
  • 嘉兴公司制作网站的如何营销
  • 个人 申请域名做网站中山seo推广优化
  • wordpress自动同步插件怀来网站seo
  • 网站建设 价格百度推广效果怎么样
  • 吉化北建公司官网西青seo
  • 做网站原创要多少钱外贸快车
  • 做网站交互demo工具唐山seo优化
  • 网站的开发环境设计美国seo薪酬