方法:1、利用原型让一个引用类型继承另外一个引用类型的属性和方法;2、借用构造函数,在子类构造函数的内部调用超类构造函数,通过用call()和apply()可在新创建的对象上执行构造函数;3、将原型链和借用构造函数的技术组合在一块实现继承。
本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。
前言:大多OO语言都支持两种继承方式: 接口继承和实现继承 ,而ECMAScript中无法实现接口继承,ECMAScript只支持实现继承,而且其实现继承主要是依靠 原型链 来实现。
1.原型链
基本思想:利用原型让一个引用类型继承另外一个引用类型的属性和方法。
构造函数,原型,实例之间的关系:每个构造函数都有一个原型对象,原型对象包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针。
原型链实现继承例子:
function SuperType() { this .property = true ; } SuperType.prototype.getSuperValue = function () { return this .property; } function subType() { this .property = false ; } //继承了SuperType SubType.prototype = new SuperType(); SubType.prototype.getSubValue = function (){ return this .property; } var instance = new SubType(); console.log(instance.getSuperValue()); //true
2.借用构造函数
基本思想:在子类型构造函数的内部调用超类构造函数,通过使用call()和apply()方法可以在新创建的对象上执行构造函数。
例子:
function SuperType() { this .colors = [ "red" , "blue" , "green" ]; } function SubType() { SuperType.call( this ); //继承了SuperType } var instance1 = new SubType(); instance1.colors.push( "black" ); console.log(instance1.colors); //"red","blue","green","black" var instance2 = new SubType(); console.log(instance2.colors); //"red","blue","green"
3.组合继承
基本思想:将原型链和借用构造函数的技术组合在一块,从而发挥两者之长的一种继承模式。
例子:
function SuperType(name) { this .name = name; this .colors = [ "red" , "blue" , "green" ]; } SuperType.prototype.sayName = function () { console.log( this .name); } function SubType(name, age) { SuperType.call( this ,name); //继承属性 this .age = age; } //继承方法 SubType.prototype = new SuperType(); Subtype.prototype.constructor = Subtype; Subtype.prototype.sayAge = function () { console.log( this .age); } var instance1 = new SubType( "EvanChen" ,18); instance1.colors.push( "black" ); consol.log(instance1.colors); //"red","blue","green","black" instance1.sayName(); //"EvanChen" instance1.sayAge(); //18 var instance2 = new SubType( "EvanChen666" ,20); console.log(instance2.colors); //"red","blue","green" instance2.sayName(); //"EvanChen666" instance2.sayAge(); //20
4.原型式继承
基本想法:借助原型可以基于已有的对象创建新对象,同时还不必须因此创建自定义的类型。
原型式继承的思想可用以下函数来说明:
function object(o) { function F(){} F.prototype = o; return new F(); }
例子:
var person = { name: "EvanChen" , friends:[ "Shelby" , "Court" , "Van" ]; }; var anotherPerson = object(person); anotherPerson.name = "Greg" ; anotherPerson.friends.push( "Rob" ); var yetAnotherPerson = object(person); yetAnotherPerson.name = "Linda" ; yetAnotherPerson.friends.push( "Barbie" ); console.log(person.friends); //"Shelby","Court","Van","Rob","Barbie"
ECMAScript5通过新增Object.create()方法规范化了原型式继承,这个方法接收两个参数:一个用作新对象原型的对象和一个作为新对象定义额外属性的对象。
var person = { name: "EvanChen" , friends:[ "Shelby" , "Court" , "Van" ]; }; var anotherPerson = Object.create(person); anotherPerson.name = "Greg" ; anotherPerson.friends.push( "Rob" ); var yetAnotherPerson = Object.create(person); yetAnotherPerson.name = "Linda" ; yetAnotherPerson.friends.push( "Barbie" ); console.log(person.friends); //"Shelby","Court","Van","Rob","Barbie"
5.寄生式继承
基本思想:创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后再像真正是它做了所有工作一样返回对象。
例子:
function createAnother(original) { var clone = object(original); clone.sayHi = function () { alert( "hi" ); }; return clone; } var person = { name: "EvanChen" , friends:[ "Shelby" , "Court" , "Van" ]; }; var anotherPerson = createAnother(person); anotherPerson.sayHi(); ///"hi"
6.寄生组合式继承
基本思想:通过借用函数来继承属性,通过原型链的混成形式来继承方法
其基本模型如下所示:
function inheritProperty(subType, superType) { var prototype = object(superType.prototype); //创建对象 prototype.constructor = subType; //增强对象 subType.prototype = prototype; //指定对象 }
例子:
function SuperType(name){ this .name = name; this .colors = [ "red" , "blue" , "green" ]; } SuperType.prototype.sayName = function (){ alert( this .name); }; function SubType(name,age){ SuperType.call( this ,name); this .age = age; } inheritProperty(SubType,SuperType); SubType.prototype.sayAge = function () { alert( this .age); }
【推荐学习:javascript高级教程】
以上是javascript是如何实现继承的详细内容。更多信息请关注PHP中文网其他相关文章!

USESTATE()ISCICIALFOROPTIMINECREACTAPPPERFORMACTACEUTOPACTONCACTONRE REDERSANDUPDATES.TOOPTIMIZE:1)USEUSECALLBACKTOMEMOEMOEIZEFUNCTIONSANDPREVENTUNNNNNNNNNNNNNNNNENESMARYRERER.2)limemememememoforcachingExpensiveComputations.3)

使用Context和useState共享状态是因为它们可以简化大型React应用中的状态管理。1)减少propdrilling,2)代码更清晰,3)更易管理全局状态。但要注意性能开销和调试复杂性,合理使用Context和优化技术可以提升应用的效率和可维护性。

使用不正确的键会导致React应用程序中的性能问题和意外行为。1)键是列表项的唯一标识符,帮助React高效地更新虚拟DOM。2)使用相同或不唯一的键会导致列表项重新排序和组件状态丢失。3)使用稳定且唯一的标识符作为键可以优化性能,避免全量重渲染。4)使用工具如ESLint来验证键的正确性。正确使用键可以确保React应用的高效和可靠性。

抗反应,KeysareSentialForoPtimizingListrenderingPerformanceByHelpingReaCreActTrackChangesinListItems.1)KeySenableFiticeFficityDomupdatesbyDatesbyIdentifyingAddedAdded,Orremervedemss.2)使用UniqueNiqueIdentifiersLikeIdentifiersLikeDataBaseIdSaskeys,而不是预测

useState在React中常被误用。1.误解useState的工作机制:setState后状态不会立即更新。2.错误更新状态:应使用函数形式的setState。3.过度使用useState:非必要时应使用props。4.忽略useEffect的依赖数组:状态变化时需更新依赖数组。5.性能考虑:批量更新状态和简化状态结构可提升性能。正确理解和使用useState能提高代码效率和可维护性。

是的,ReactApplicationsCanbEseo-FrylylywithProperStratecies.1)用户 - 插图(SSR)withToolslikenext.jstogenate.jstogenate fullhtmlforindexing.2)enasleStaticsiteSitegeneration(ssg)

React性能瓶颈主要由低效渲染、不必要的重渲染和组件内重的计算造成。 1)使用ReactDevTools定位慢组件并应用React.memo优化。 2)优化useEffect,确保仅在必要时运行。 3)使用useMemo和useCallback进行记忆化处理。 4)将大组件拆分为小组件。 5)对于大数据列表,使用虚拟滚动技术优化渲染。通过这些方法,可以显着提升React应用的性能。

有人可能会寻找React的替代品,因为性能问题、学习曲线或探索不同的UI开发方法。1)Vue.js因其易于集成和温和的学习曲线而受到赞扬,适用于小型和大型应用。2)Angular由Google开发,适合大型应用,具有强大的类型系统和依赖注入。3)Svelte通过在构建时编译成高效的JavaScript,提供出色的性能和简洁性,但其生态系统仍在成长。选择替代品时,应根据项目需求、团队经验和项目规模来决定。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

WebStorm Mac版
好用的JavaScript开发工具