草庐IT

Inheritance

全部标签

javascript - Babel 已经做了 Object.create(superClass.prototype) 为什么还要用 setPrototypeOf 来继承?

将以下代码发布到BabelREPLclassTest{}classTest2extendsTest{}你得到了这个inherits函数function_inherits(subClass,superClass){if(typeofsuperClass!=="function"&&superClass!==null){thrownewTypeError("Superexpressionmusteitherbenullorafunction,not"+typeofsuperClass);}subClass.prototype=Object.create(superClass&&superC

javascript - 使用 Object.create(null) 创建对象时 __proto__ 如何工作

考虑以下javascript代码vara=Object.create(null);a.foo=1;varb=Object.create(a);console.log(b.foo);//prints1console.log(b.__proto__);//printsundefinedb.__proto__=null;console.log(b.__proto__);//printsnullconsole.log(b.foo);//prints1即使在将b.__proto__设置为null之后,谁能解释对象b如何访问a的“foo”属性?用于访问a属性的内部链接是什么?我尝试在SO中搜索可能

javascript - 原型(prototype)继承。这个简单的例子有什么问题?

functiona(){this.testing='testing';}functionb(){}b.prototype=newa();console.log(b.testing);控制台显示未定义,而不是“测试”。我做错了什么? 最佳答案 您还没有创建'b'的实例。varbInstance=newb();console.log(bInstance.testing);换句话说,原型(prototype)的属性只出现在b类型的对象上,而不是b()构造函数本身。 关于javascript-原

javascript - backbone.js View 继承。 `this` 父级分辨率

我有一个使用View继承的案例,我的代码基本上是这样的:parentView=Backbone.View.extend({events:{"someevent":"business"},initialize:function(){_.bindAll(this);},business:function(e){...this.someFunc&&this.someFunc();...}});childView=parentView.extend({events:{...},constructor:function(){this.events=_.extend({},parentView.p

javascript - 从子类调用父类(super class)方法 - JavaScript

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:setattributewithjavascriptsupermethod为了好玩,我正在尝试用HTML5创建一个简单的游戏。我有一个Entity类,它应该是Player类的父类(superclass)。functionEntity(x,y){this.x=x;this.y=y;this.tick=function(){//Dogenericstuff}}functionPlayer(x,y){this.parent.constructor.call(this,x,y);this.tick=function(

javascript - 继承和模块模式

我正在尝试以这种方式使用模块模式实现继承:Parent=function(){//constructor(functionconstruct(){console.log("Parent");})();//publicfunctionsreturnthis.prototype={test:function(){console.log("testparent");},test2:function(){console.log("test2parent");}};};Child=function(){//constructor(function(){console.log("Child");P

javascript - 继承自 jQuery UI 对话框并调用覆盖的方法

下面的简单代码描述了我的问题(至少我希望如此):$.widget("ui.mydialog",$.ui.dialog,{_create:function(){//Howtocall_createmethodofdialog?}});我试图从上面的创建方法中调用$.ui.dialog.prototype._create(),但在Firebug中出现以下错误:this.elementisundefinedthis.originalTitle=this.element.attr('title');jquery...5667348(line5864)我还能如何称呼该“super”方法?jQue

javascript - JavaScript 中的原型(prototype)继承约定

我看到很多这样的代码:functionBase(){}functionSub(){}Sub.prototype=newBase();但是,如果您这样做:s=newSub();print(s.constructor==Sub);这是错误的。这让我感到困惑,因为s的构造函数确实是Sub。这样做是传统的/更好的吗?functionBase(){}functionSub(){}Sub.prototype=newBase();Sub.prototype.constructor=Sub;还是真的不重要? 最佳答案 'constructor'并不

javascript - JS中的继承 : this. base = Class(); this.base() 还是……?

我试图在JS中“获得”继承。我刚刚发现了一种基本上可以将所有属性从一个对象复制到另一个对象的简洁方法:functionPerson(name){this.name="MrorMiss:"+name;this.introduce=function(){console.log("Hi,Iam"+this.name);}}functionEmployee(name,title){this.title=title;this.base=Person;this.base(name);}e=newEmployee('tony','manager')e.introduce();请注意,我有一个带有构造

javascript - 关于 JavaScript 中的继承的问题

你能解释一下下面提到的两个代码之间的区别吗?functionPerson(){}Person.prototype.dance=function(){};functionNinja(){}Ninja.prototype=Person.prototype;和functionPerson(){}Person.prototype.dance=function(){};functionNinja(){}Ninja.prototype=newPerson();我对这些行有点困惑:Ninja.prototype=Person.prototype;和Ninja.prototype=newPerson(