草庐IT

super-constructor

全部标签

javascript - 不要用 new Constructor 创建对象

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

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

super 和原型(prototype)之间的Javascript区别

我是javaScript的新手,我正在做一些继承。我以为我知道原型(prototype)是什么,但后来我遇到了super方法。现在我不知道这两者之间的区别。我知道uber就像java中的super,仅此而已。但是原型(prototype)是困扰我的事情。如果您能给我一些使用这两者的简单示例,我将不胜感激。 最佳答案 uber只是DouglasCrockford在他的JavaScript继承示例中创建的一个糖方法,在使用JavaScript原型(prototype)继承的非常非常灵活的特性时,它应该有助于开发人员。nativeJava

javascript - ES6 super() 在构造函数中实际上做了什么?

!你好,friend们。我有这个小类继承结构classPoint{constructor(x,y){this.x=x;this.y=y;}toString(){return'('+this.x+','+this.y+')';}}classColorPointextendsPoint{constructor(x,y,color){super(x,y);this.color=color;}toString(){returnsuper.toString()+'in'+this.color;}}letnewObj=newColorPoint(25,8,'green');它编译为thisjsfi

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

javascript - 有没有办法在 super 测试中设置默认值?

我正在使用supertest模块来测试我的RestAPI。我的API一直发送JSON。所以我正在为所有和每个测试做.expect('Content-Type',/json/)!我一次又一次地重复!这是我的一些代码it('shouldlistALLpermissionson/permissionsGET',(done)=>{request(app).get(permissionsURL).expect(200).expect('Content-Type',/json/).end((err,res)=>{varpermissions=res.body;permissions.should.

javascript - React 中的 Constructor(props) 和 super(props) VS constructor() 和 super()

我知道这个问题被问过很多次了,但还是不清楚。很多人只是说:Passpropstoconstructorifyouwanttoaccessthis.propsthereonemoreexampleoftheanswer官方文档说Classcomponentsshouldalwayscallthebaseconstructorwithprops.,但如果我们不将props传递给constructor,除了构造函数之外,我们仍然会在任何地方都有this.props。同样来自reactsourcecode我们可以看到React.Component的源代码函数ReactComponent(pro

javascript - new super.constructor 是 JavaScript 中的有效表达式吗?

我想知道newsuper.SomeProperty形式的表达式在JavaScript中是否有效。这个问题是在处理跨浏览器行为不一致的代码时出现的,例如下面代码片段中的示例。classTest{test(){returnnewsuper.constructor;}}console.log((newTest).test());这会在Firefox和Edge中打印一个空对象,但会在Chrome中抛出一个ReferenceError并在Safari中抛出一个SyntaxError。通过在super.constructor周围加上括号或使用变量可以很容易地避免该错误,因此这不是真正的限制,而是对

javascript - 如何在 Crockford 的新构造函数模式中共享 "constructor"功能?

我看过来自thisquestiononclass-freeOOP的视频现在好几次了,但我无法将其应用到现实世界的示例中。Crockford的新构造函数模式如下所示:functionconstructor(spec){let{member}=spec,{other}=other_constructor(spec),method=function(){//accessesmember,other,method,spec};returnObject.freeze({method,other,});}其中spec是一个选项散列,生成的对象公开了关闭所有内部成员的方法。忽略解构(因为这可以在当今