jjzjj

Constructor

全部标签

javascript - fabric.Canvas 不是构造函数

我像这样包含了织物:但是当我这样使用它时:varcanvas;functioninitSketchPad(){canvas=newfabric.Canvas('sketch-pad',{isDrawingMode:true});}我明白了UncaughtTypeError:fabric.Canvasisnotaconstructor 最佳答案 我不确定您的fabric_freedrawing.js文件中有什么,但将您的代码添加到下面的代码片段似乎工作正常。varcanvas;functioninitSketchPad(){canva

javascript - 有没有可能在调用构造函数之前就知道 'this'这个对象呢?

在ES6类之前,函数可以用作构造函数:functionMyClass(a,b){}那么,下面的代码就相当于一个经典的实例化(比如letthisObj=newMyClass("A","B")):letthisObj=Object.create(MyClass.prototype)//Hereweknowthe`this`objectbeforetocalltheconstructor.//Then,theconstructoriscalledmanually:MyClass.call(thisObj,"A","B")...这种技术是一种在调用构造函数之前了解this对象的方法。但是Fun

javascript - 从javascript中的构造函数显式返回值

http://ejohn.org/blog/building-a-javascript-library/在上面的链接中,如果调用者最初忘记了,JohnResig建议在构造函数中调用并返回newfoo。这对我来说有些道理,但后来我得到了一个严格的错误,因为我的构造函数并不“总是”返回一个值。在对javascript中的构造函数有了一点了解后,我不再返回this,因为new会自动返回。我的问题是,我应该...不使用描述的防御技术?在我的构造函数结束时返回它?我不知道的神秘选项? 最佳答案 返回this是没有意义的,因为如果调用者忘记添加

javascript - 从错误中恢复

在我因为如此鲁莽地尝试做事而被大吼大叫之前,让我告诉你,我在现实生活中不会这样做,这是一个学术问题。假设我正在编写一个库,并且我希望我的对象能够根据需要组成方法。例如,如果你想调用一个.slice()方法,而我没有,那么window.onerror处理程序会为我触发它不管怎样,我都玩过这个herewindow.onerror=function(e){varmethod=/'(.*)'$/.exec(e)[1];console.log(method);//slicereturnArray.prototype[method].call(this,arguments);//notevenal

javascript - 原型(prototype)继承的差异,Firefox 与 Chrome

对于下面的代码:functionMammal(){this.hair=true;this.backbone=true;returnthis;}functionCanine(){this.sound='woof';returnthis;}Canine.prototype=newMammal();functionDog(name){this.tail=true;this.name=name;returnthis;}Dog.prototype=newCanine();varaspen=newDog('Aspen');varaspenProto=aspen.__proto__Firebug(F

console.log 中的 JavaScript 对象输出

我想知道打印对象时console.log从哪里得到构造函数的名字。另外,这实际上对代码有什么影响吗?functionF(){this.test='ok';}varf=newF();console.log(f);console.log(在Chrome中)的输出是:F{测试:“确定”}console.log从哪里得到F{test...中的F?如果我将F.constructor、F.prototype和f.constructor更改为随机值,它仍会打印原始的F:functionG(){this.fail='bad';}functionF(){this.test='ok';}F.prototy

javascript - 难以手动走原型(prototype)链

我想尝试手动遍历几个对象的原型(prototype)链,看看我在这个过程中找到了什么。但是,我卡在了我尝试的第一个上。这是代码:functionMyObject(){}varx=newMyObject();console.log('--------------------------------------------');console.log('x.constructor.name:'+x.constructor.name);console.log('x.constructor.prototype.constructor.name:'+x.constructor.prototype

javascript - Webpack 在使用继承缩小/丑化 ES6 代码时删除了类名

Webpack在使用继承缩小/丑化ES6代码时删除了类名:有MVCE我们尝试缩小/丑化的代码:子类:constParentClass=require('parent');classChildextendsParentClass{constructor(){super();}}module.exports=Child;index.js调用Child类:constChild=require('./classes_so/child');letchild=newChild();console.log(child.constructor.name);node_modules中的ModulePar

javascript - 如何在不同的上下文中使用 es6 构造函数指令

是否可以通过更改“this”上下文(调用、应用或其他)在另一个实例上使用es6构造函数指令?这可以使用es5“类”。这是我的意思的一个小例子:functionES5(){this.foo='foo';}classES6{constructor(){this.bar='bar';}}vara=newES6();ES5.call(a);console.log(a.foo+a.bar);//foobarvarb=newES5();//Reflect.construct(ES6);??ES6.call(b);//TypeError:ClassconstructorES6cannotbeinvo

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做什么或应该做什么。除非我遗漏了什么,否则我使用示例代码得到的输出是