草庐IT

inheriting-constructors

全部标签

javascript - 为什么 MDN 的 `Object.create` polyfill 没有设置 `prototype.constructor` ?

考虑MDN'sObject.createpolyfill:if(typeofObject.create!='function'){(function(){varF=function(){};Object.create=function(o){if(arguments.length>1){throwError('Secondargumentnotsupported');}if(o===null){throwError('Cannotsetanull[[Prototype]]');}if(typeofo!='object'){throwTypeError('Argumentmustbean

javascript - Web 开发人员专业 JavaScript 中的 "Parasitic Combination Inheritance"

Web开发人员专业JavaScript,第三版,NicholasC.Zakas(Wrox,2012年,第210-215页描述了使用以下函数的“寄生组合继承”:functioninheritPrototype(subType,superType){varprototype=object(superType.prototype);prototype.constructor=subType;subType.prototype=prototype;}我还没有弄清楚将subType分配给prototype.constructor做什么或应该做什么。除非我遗漏了什么,否则我使用示例代码得到的输出是

javascript - JS : Confusion about inheritance

我通过C++、Java等语言熟悉OOP概念。现在我正在尝试将JavaScript作为一种爱好来学习,主要是出于对WebGL的兴趣。但是我在基于原型(prototype)的继承方面遇到了麻烦。假设我有一个基类,它在构造函数中接受一个参数。我需要扩展它。我这样做的方式如下所示。functionBase(n){this._n=n;}Base.prototype.print=function(){console.log(this._n);}functionDerived(n){Base.call(this,n);}Derived.prototype=newBase;Derived.protot

javascript - 我可以使用 constructor.name 来检测 JavaScript 中的类型吗

我可以使用“constructor”属性来检测JavaScript中的类型吗?或者有什么我应该知道的。例如:vara={};a.构造函数名称;//输出“对象”或varb=1;b.构造函数名称;//输出“数字”或vard=newDate();d.构造函数名称;//输出“日期”而不是对象或varf=newFunction();f.构造函数名称;//输出“函数”而不是对象只有在参数上使用它时arguments.constructor.name;//像第一个例子一样输出对象我经常看到开发人员使用:Object.prototype.toString.call([])或Object.prototy

javascript - 为什么要将 Something 分配给 Something.prototype.constructor?

我正在阅读有关Javascript原型(prototype)属性如何与继承一起工作的内容,然后开始查看Angular.js代码并提出了一些问题。首先,我读到原型(prototype)属性指向一个对象,该对象具有一个“构造函数”属性,该属性指向用于创建该对象的原始函数。例如://ThisistheconstructorfunctionShape(){this.position=1;}//TheconstructorpointsbacktotheoriginalfunctionwedefinedShape.protoype.constructor==Shape;原型(prototype)还

javascript - 不要用 new Constructor 创建对象

是否可以选择不在构造函数中创建具有特定条件的对象,例如functionMonster(name,hp){if(hp 最佳答案 我认为你应该做的是抛出一个异常。functionMonster(name,hp){if(hp 关于javascript-不要用newConstructor创建对象,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/15355908/

javascript - 需要模式 : create new object that returns an executeable function and inherits from a prototype

场景1-一切正常:varAwesomeObject=function(){varself=this;self.whatstuff='reallyawesome';}AwesomeObject.prototype.doStuff=function(){varself=this;console.log('idid'+self.whatstuff+'stuff');returnself;}varawesome=newAwesomeObject();//returnsanewAwesomeObjectawesome.doStuff();//prints'ididreallyawesomestu

javascript - 收到 "No parameterless constructor defined"错误,不确定原因

出于某种原因,我的一个特定AJAX调用出现“未定义无参数构造函数”错误。这是代码:CallAndReplace(JSON.stringify(model),url,$("#panel"));functionCallAndReplace(data,url,replace){$.ajax({url:url,type:"post",contentType:"application/json;charset=utf-8",data:data,success:function(result){replace.html(result);},error:function(x,e){if(x.stat

javascript - 如何修复 "TypeError: fsevents is not a constructor" react 错误

在使用npmstart启动React应用程序时发生此错误...我试过下面的东西:删除node_module包并重新安装使用yarn代替npm3.使用npm更新fsevent库仍然出现这个错误注意:如果我们创建React应用程序fresh/newyarnstart会工作,但我们关闭终端并使用以下相同的东西重新启动会发生以下错误/Users/nannam/test-app-2/node_modules/chokidar/lib/fsevents-handler.js:28return(newfsevents(path)).on('fsevent',callback).start();^Ty

javascript - 当 myarray 在一个框架中时,为什么 myarray instanceof Array 和 myarray.constructor === Array 都为 false?

所以下面的代码会发出两次错误警报:window.onload=function(){alert(window.myframe.myarrayinstanceofArray);alert(window.myframe.myarray.constructor===Array);}当页面中有一个名为“myframe”的iframe包含一个名为“myarray”的数组时。如果数组被移动到主页(而不是iframe),那么代码会像预期的那样发出两次true警报。有谁知道这是为什么吗? 最佳答案 functionisArray(o){return