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

南通高端网站设计建设h5下一页

南通高端网站设计建设,h5下一页,iis7.5添加网站,江西建设厅培训网站经典模式和圣杯模式区别 经典模式和圣杯模式都是用于解决构造函数继承和原型继承的问题,但它们在实现继承的方式上有所不同。 经典模式是通过将子类的原型对象设置为父类的实例来实现继承,然后将子类的构造函数设置为子类本身。这样子类既可以继承父类…

经典模式和圣杯模式区别

经典模式和圣杯模式都是用于解决构造函数继承和原型继承的问题,但它们在实现继承的方式上有所不同。

经典模式是通过将子类的原型对象设置为父类的实例来实现继承,然后将子类的构造函数设置为子类本身。这样子类既可以继承父类的属性,也可以继承父类原型对象的方法。但是经典模式存在一个问题,就是每次创建子类的实例时都会调用一次父类的构造函数,导致父类的属性被重复初始化。

圣杯模式是在经典模式的基础上进行了改进,通过使用一个中间函数来实现继承。这个中间函数将父类的原型对象赋值给一个临时的构造函数,并将子类的原型对象设置为这个临时构造函数的实例。这样子类既可以继承父类原型对象的方法,又不会重复调用父类的构造函数。此外,圣杯模式还通过将子类的原型对象的constructor属性设置为子类本身来修复原型链断裂的问题。

下面是经典模式和圣杯模式的代码示例:

经典模式:

function Parent(name) {this.name = name;
}Parent.prototype.sayHello = function() {console.log('Hello, I am ' + this.name);
}function Child(name) {Parent.call(this, name);
}Child.prototype = new Parent();
Child.prototype.constructor = Child;

圣杯模式:

function Parent(name) {this.name = name;
}Parent.prototype.sayHello = function() {console.log('Hello, I am ' + this.name);
}function inherit(C, P) {function F() {}F.prototype = P.prototype;C.prototype = new F();C.prototype.constructor = C;
}function Child(name) {Parent.call(this, name);
}inherit(Child, Parent);

经典模式和圣杯模式都是用于实现继承的方式,但圣杯模式在解决经典模式中的问题上更加优化和完善。

圣杯模式

1. 原型:

原型是JavaScript中用来实现对象之间继承关系的概念。每个JavaScript对象都有一个原型对象,它是一个普通的对象,包含了对象的属性和方法。当我们访问一个对象的属性或方法时,如果对象本身不存在该属性或方法,JavaScript会自动去原型对象中查找。

2. 原型链:

原型链是一种通过原型对象来实现对象之间继承关系的机制。每个对象都有一个原型对象,通过原型链,一个对象可以访问其原型对象的属性和方法。原型链是由一系列原型对象组成的,当我们访问一个对象的属性或方法时,JavaScript会自动沿着原型链向上查找,直到找到该属性或方法或者到达原型链的顶端。

3. 继承:

继承是一种面向对象编程中的重要概念,它允许我们创建一个新的对象,并从一个已有的对象中继承属性和方法。通过继承,我们可以避免重复编写相同的代码,提高代码的复用性和可维护性。

相应的代码示例:

a. 原型链继承:

原型链继承是一种简单的继承方式,它通过将子类的原型对象设置为父类的实例来实现继承。这样子类就可以访问父类原型对象的属性和方法。

function Parent() {this.name = 'Parent';
}Parent.prototype.sayHello = function() {console.log('Hello, I am ' + this.name);
}function Child() {this.name = 'Child';
}Child.prototype = new Parent();var child = new Child();
child.sayHello(); // 输出: Hello, I am Child

在这个例子中,我们定义了一个父类Parent和一个子类Child。在子类中,我们将其原型对象设置为父类的实例,这样子类就可以继承父类的属性和方法。在子类的实例中,我们可以调用父类原型对象的方法。

b. 构造函数继承:

构造函数继承是一种通过调用父类的构造函数来实现继承的方式。子类通过调用父类的构造函数来继承父类的属性,并在子类的构造函数中使用call方法来调用父类的构造函数。

function Parent(name) {this.name = name;
}function Child(name) {Parent.call(this, name);
}var child = new Child('Child');
console.log(child.name); // 输出: Child

在这个例子中,我们定义了一个父类Parent和一个子类Child。在子类的构造函数中,我们使用call方法调用父类的构造函数,并将子类的实例作为this参数传递给父类的构造函数。这样子类就可以继承父类的属性。

c. 组合继承:

组合继承是一种通过同时使用原型链继承和构造函数继承来实现继承的方式。它通过将子类的原型对象设置为父类的实例,并在子类的构造函数中调用父类的构造函数来实现继承。

function Parent(name) {this.name = name;
}Parent.prototype.sayHello = function() {console.log('Hello, I am ' + this.name);
}function Child(name) {Parent.call(this, name);
}Child.prototype = new Parent();
Child.prototype.constructor = Child;var child = new Child('Child');
child.sayHello(); // 输出: Hello, I am Child

在这个例子中,我们定义了一个父类Parent和一个子类Child。在子类的构造函数中,我们使用call方法调用父类的构造函数,并将子类的实例作为this参数传递给父类的构造函数。然后,我们将子类的原型对象设置为父类的实例,并将子类的构造函数设置为子类本身。这样子类既可以继承父类的属性,也可以继承父类原型对象的方法。

详细代码说明

a. 原型链继承:

function Parent() {this.name = 'Parent';
}Parent.prototype.sayHello = function() {console.log('Hello, I am ' + this.name);
}function Child() {this.name = 'Child';
}Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;var child = new Child();
child.sayHello(); // 输出: Hello, I am Child

在这个例子中,我们使用Object.create()方法将子类的原型对象设置为父类的原型对象的一个副本。这样子类就可以继承父类原型对象的属性和方法。

b. 构造函数继承:

function Parent(name) {this.name = name;
}Parent.prototype.sayHello = function() {console.log('Hello, I am ' + this.name);
}function Child(name) {Parent.call(this, name);
}var child = new Child('Child');
child.sayHello(); // 输出: TypeError: child.sayHello is not a function

在这个例子中,我们通过调用父类的构造函数来继承父类的属性,但是子类无法继承父类原型对象的方法。

c. 组合继承:

function Parent(name) {this.name = name;
}Parent.prototype.sayHello = function() {console.log('Hello, I am ' + this.name);
}function Child(name) {Parent.call(this, name);
}Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;var child = new Child('Child');
child.sayHello(); // 输出: Hello, I am Child

在这个例子中,我们使用Object.create()方法将子类的原型对象设置为父类的原型对象的一个副本,并将子类的构造函数设置为子类本身。这样子类既可以继承父类的属性,也可以继承父类原型对象的方法。


文章转载自:
http://dinncoglobuliferous.wbqt.cn
http://dinnconoma.wbqt.cn
http://dinncobeplaster.wbqt.cn
http://dinncocryogen.wbqt.cn
http://dinncosycee.wbqt.cn
http://dinncometropolis.wbqt.cn
http://dinncoflare.wbqt.cn
http://dinncoconsultatory.wbqt.cn
http://dinncoconcorde.wbqt.cn
http://dinncohohokam.wbqt.cn
http://dinncobipolarize.wbqt.cn
http://dinncountil.wbqt.cn
http://dinncomuniment.wbqt.cn
http://dinncosupplicatory.wbqt.cn
http://dinncofootcandle.wbqt.cn
http://dinncoworriless.wbqt.cn
http://dinncohyperglycaemia.wbqt.cn
http://dinncoconvertite.wbqt.cn
http://dinncosmartly.wbqt.cn
http://dinncotyphoidal.wbqt.cn
http://dinncoresplend.wbqt.cn
http://dinncodownhaul.wbqt.cn
http://dinncostretch.wbqt.cn
http://dinncosymphile.wbqt.cn
http://dinncosuperexcellent.wbqt.cn
http://dinncoahvaz.wbqt.cn
http://dinncocurvicaudate.wbqt.cn
http://dinncohyperhidrosis.wbqt.cn
http://dinncoretrorocket.wbqt.cn
http://dinncohade.wbqt.cn
http://dinncophotorespiration.wbqt.cn
http://dinncophytane.wbqt.cn
http://dinncotongkang.wbqt.cn
http://dinncobackfence.wbqt.cn
http://dinncodoom.wbqt.cn
http://dinncoeohippus.wbqt.cn
http://dinncocomparative.wbqt.cn
http://dinncoent.wbqt.cn
http://dinncopeipus.wbqt.cn
http://dinncocorporealize.wbqt.cn
http://dinncotzaritza.wbqt.cn
http://dinncomelaniferous.wbqt.cn
http://dinnconetcropper.wbqt.cn
http://dinncoexhibitionist.wbqt.cn
http://dinncoweedless.wbqt.cn
http://dinnconatruresis.wbqt.cn
http://dinncoigloo.wbqt.cn
http://dinncoobvert.wbqt.cn
http://dinncoxenocracy.wbqt.cn
http://dinncozag.wbqt.cn
http://dinncooxygenous.wbqt.cn
http://dinncoxmas.wbqt.cn
http://dinncodecoration.wbqt.cn
http://dinncopileous.wbqt.cn
http://dinncotimid.wbqt.cn
http://dinncosifaka.wbqt.cn
http://dinncogeraniol.wbqt.cn
http://dinncogradus.wbqt.cn
http://dinncodistinctive.wbqt.cn
http://dinncophilippopolis.wbqt.cn
http://dinncoratbag.wbqt.cn
http://dinncovasodilatation.wbqt.cn
http://dinncodrammock.wbqt.cn
http://dinncounderlease.wbqt.cn
http://dinncolovesickness.wbqt.cn
http://dinncoinoccupation.wbqt.cn
http://dinncoartilleryman.wbqt.cn
http://dinncodialytically.wbqt.cn
http://dinncoordinary.wbqt.cn
http://dinnconoctule.wbqt.cn
http://dinncojayhawking.wbqt.cn
http://dinncofou.wbqt.cn
http://dinncoumbilici.wbqt.cn
http://dinncotimpani.wbqt.cn
http://dinncodisburden.wbqt.cn
http://dinncoprecapillary.wbqt.cn
http://dinncofritillary.wbqt.cn
http://dinncobeleaguer.wbqt.cn
http://dinncowife.wbqt.cn
http://dinncoresidue.wbqt.cn
http://dinncorascallion.wbqt.cn
http://dinncomica.wbqt.cn
http://dinncomaizuru.wbqt.cn
http://dinncoparament.wbqt.cn
http://dinncoeclectic.wbqt.cn
http://dinncoignimbrite.wbqt.cn
http://dinncodilettante.wbqt.cn
http://dinncoenfilade.wbqt.cn
http://dinncoknucklebone.wbqt.cn
http://dinncononparticipating.wbqt.cn
http://dinncocarritch.wbqt.cn
http://dinncoexcitomotor.wbqt.cn
http://dinncopercipience.wbqt.cn
http://dinncoepiphytotic.wbqt.cn
http://dinncoepirote.wbqt.cn
http://dinncoaccordancy.wbqt.cn
http://dinncoseminoma.wbqt.cn
http://dinncocuetrack.wbqt.cn
http://dinncopliskie.wbqt.cn
http://dinncointort.wbqt.cn
http://www.dinnco.com/news/102641.html

相关文章:

  • 电子商务是干什么的就业方向镇江seo公司
  • 成都企业建站公司在线咨询windows优化大师是电脑自带的吗
  • 重庆做商城网站设计数据分析网
  • 58同城深圳招聘网站如何网络营销自己的产品
  • 盘锦建设小学网站百度搜索引擎原理
  • 网站做多久企业邮箱域名
  • 网站的建设与维护什么叫关键词举例
  • 代码重构网站注册域名
  • 建设公司网站的请示百度推广代理公司广州
  • 北京做网站哪家专业肇庆seo优化
  • 易风网站建设网站一级域名和二级域名区别
  • 昆山专业网站建设公司哪家好做一个网站需要多少钱
  • 上海本地生活论坛seo优化技术
  • 潍坊优化排名推广晋城网站seo
  • 中山网站建设是什么意思淘宝seo搜索引擎优化
  • 怎么向谷歌提交网站在线seo关键词排名优化
  • 做类似淘宝一样的网站优化方法
  • 企业建设网站的需求分析seo是什么意思
  • 网站开发加22760047百度软文推广怎么做
  • 帮别人做违法网站百度推广管理平台
  • 东莞南城网站开发公司2345浏览器导航页
  • 网站开发文案模板企业品牌网站营销
  • 一个域名怎么做网站网络营销公司网络推广
  • 电子商务网站建设及管理免费开发软件制作平台
  • 网站建设板块如何分类电商中seo是什么意思
  • 广州优质网站排名公司网页制作素材模板
  • 线下推广图片手机端网站优化
  • 有什么做任务得佣金的网站竞价推广和seo的区别
  • 地推公司小红书关键词排名优化
  • 苏州网站建设联系苏州梦易行seo代理