unn*_*nni 87 javascript constructor object typeerror
我正在定义一个这样的对象:
function Project(Attributes, ProjectWidth, ProjectHeight)
{
this.ProjectHeight = ProjectHeight;
this.ProjectWidth = ProjectWidth;
this.ProjectScale = this.GetProjectScale();
this.Attributes = Attributes;
this.currentLayout = '';
this.CreateLayoutArray = function()
{....}
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试创建和实例像这样:
var newProj = new Project(a,b,c);
Run Code Online (Sandbox Code Playgroud)
但是这个例子被抛出:
Project is not a constructor
Run Code Online (Sandbox Code Playgroud)
可能有什么不对?我搜索了很多,但仍然无法弄清楚我做错了什么.
Rob*_*b W 79
发布在问题中的代码无法生成该错误,因为Project它不是用户定义的函数/有效构造函数.
function x(a,b,c){}
new x(1,2,3); // produces no errors
Run Code Online (Sandbox Code Playgroud)
你可能做过这样的事情:
function Project(a,b,c) {}
Project = {}; // or possibly Project = new Project
new Project(1,2,3); // -> TypeError: Project is not a constructor
Run Code Online (Sandbox Code Playgroud)
使用的变量声明var被提升,因此总是在其余代码之前进行评估.所以,这也可能导致问题:
function Project(){}
function localTest() {
new Project(1,2,3); // `Project` points to the local variable,
// not the global constructor!
//...some noise, causing you to forget that the `Project` constructor was used
var Project = 1; // Evaluated first
}
Run Code Online (Sandbox Code Playgroud)
wpr*_*prl 46
另一个原因可能是ES2015 箭头功能. 它们不能用作构造函数.
const f = () => {};
new f(); // This throws "f is not a constructor"
Run Code Online (Sandbox Code Playgroud)
Ric*_*ber 26
对我来说,这是ES6 之间import和之间的差异require.
例如
// processor.js
class Processor {
}
export default Processor
//index.js
const Processor = require('./processor');
const processor = new Processor() //fails with the error
import Processor from './processor'
const processor = new Processor() // succeeds
Run Code Online (Sandbox Code Playgroud)
Ber*_*rgi 21
我也用Google搜索并找到了这个解决方案:
你有一个Project不是函数的变量.然后new操作员会抱怨它.尝试console.log(Project)使用它作为构造工具的地方,你会发现它.
Edw*_*ets 11
对于我的项目,问题结果是由require()调用创建的循环引用:
y.js:
var x = require("./x.js");
var y = function() { console.log("result is " + x(); }
module.exports = y;
x.js:
var y = require("./y.js");
var my_y = new y(); // <- TypeError: y is not a constructor
var x = function() { console.log("result is " + my_y; }
module.exports = x;
Run Code Online (Sandbox Code Playgroud)
原因是当它试图初始化y时,它在依赖系统中创建了一个临时的"y"对象(不是类,对象!),它在某种程度上还不是构造函数.然后,当x.js完成定义时,它可以继续制作ya构造函数.只有,x.js在其中尝试使用非构造函数y时出错.
小智 10
在我的情况下,我使用原型名称作为对象名称.例如
function proto1()
{}
var proto1 = new proto1();
Run Code Online (Sandbox Code Playgroud)
这是一个愚蠢的错误,但可能对像我这样的人有帮助;)
Geo*_*fer 10
我在一个文件中有一个类,我要导入到测试文件中:
//Vec.js
class Vec {
}
module.exports.Vec = Vec;
Run Code Online (Sandbox Code Playgroud)
改变
//Vec.test.js
const Vec = require('./Vec');
const myVec = new Vec(); //TypeError: Vec is not a constructor
Run Code Online (Sandbox Code Playgroud)
到
//Vec.test.js
const {Vec} = require('./Vec');
const myVec = new Vec(); //Succeeds!
Run Code Online (Sandbox Code Playgroud)
为我解决了这个错误。
小智 6
Car.js
class Car {
getName() {return 'car'};
}
export default Car;
Run Code Online (Sandbox Code Playgroud)
测试文件.js
const object = require('./Car.js');
const instance = new object();
Run Code Online (Sandbox Code Playgroud)
错误:类型错误:实例不是构造函数
打印对象的内容
object = {default: Car}
Run Code Online (Sandbox Code Playgroud)
将默认值附加到 require 函数,它将作为构造函数工作
const object = require('object-fit-images').default;
const instance = new object();
instance.getName();
Run Code Online (Sandbox Code Playgroud)
有时,这只是您导出和导入它的方式。对于此错误消息,可能是缺少default关键字。
export default SampleClass {}
Run Code Online (Sandbox Code Playgroud)
在哪里实例化它:
import SampleClass from 'path/to/class';
let sampleClass = new SampleClass();
Run Code Online (Sandbox Code Playgroud)
选项 2,带花括号:
export SampleClass {}
import { SampleClass } from 'path/to/class';
let sampleClass = new SampleClass();
Run Code Online (Sandbox Code Playgroud)
要添加@wprl的答案,ES6对象方法简写,如箭头函数,也不能用作构造函数。
const o = {
a: () => {},
b() {},
c: function () {}
};
const { a, b, c } = o;
new a(); // throws "a is not a constructor"
new b(); // throws "b is not a constructor"
new c(); // works
Run Code Online (Sandbox Code Playgroud)
我只是想补充一点,如果从不同的文件调用构造函数,那么就像忘记导出构造函数一样简单
module.exports = NAME_OF_CONSTRUCTOR
Run Code Online (Sandbox Code Playgroud)
也会导致“不是构造函数”异常。