构造函数第二次失败,但不是第一次
Constructor fails on second time, but not first
我有以下代码:
var foo = function () {
foo = this;
foo.boo = function () {
console.log("boo");
}
}
var bar = new foo().boo();
var baz = new foo().boo();
这段代码第一次创建了 foo
的实例,但第二次失败,输出如下:
boo
/Users/BaranSkistad/Code/example.js:9
var baz = new foo().boo();
^
TypeError: foo is not a constructor
at Object.<anonymous> (/Users/BaranSkistad/Code/example.js:9:11)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Function.Module.runMain (module.js:609:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:578:3
为什么这个脚本失败了?我知道这与在第 2 行将 foo 设置为 this
有关,而不仅仅是使用 this
,但为什么会出现问题?
var foo = function () {
self = this;
self.boo = function () {
console.log("boo");
}
}
var bar = new foo().boo();
var baz = new foo().boo();
如果我设置self
等于this
,它通过了,为什么会这样?
问题出在下一行
foo = this;
上面一行遗漏了变量声明。所以 foo
将引用全局变量 foo
这是一个 constructor.When 你在该行第一次执行时调用该函数并且 foo
更改为 this
(实例foo
)
在第二种情况下,代码还创建了一个全局变量 self
,它将等于 this
。但在那种情况下,它不会更改构造函数,因为名称不同。
解决方法是使用let
(或const/var
)使foo
成为局部变量。
var foo = function () {
let foo = this;
foo.boo = function () {
console.log("boo");
}
}
var bar = new foo().boo();
var baz = new foo().boo();
虽然您已经得到了答案,但您可能不会明确设置this
。如果您在 foo
函数中 console.log
,它将记录一个 object
。相反,您可以 return
一个 object
,它将具有所有内部功能
var foo = function() {
return {
testVal: 5,
boo: function() {
console.log("boo ", this.testVal);
}
}
}
var bar = new foo().boo();
var baz = new foo().boo();
我有以下代码:
var foo = function () {
foo = this;
foo.boo = function () {
console.log("boo");
}
}
var bar = new foo().boo();
var baz = new foo().boo();
这段代码第一次创建了 foo
的实例,但第二次失败,输出如下:
boo
/Users/BaranSkistad/Code/example.js:9
var baz = new foo().boo();
^
TypeError: foo is not a constructor
at Object.<anonymous> (/Users/BaranSkistad/Code/example.js:9:11)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Function.Module.runMain (module.js:609:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:578:3
为什么这个脚本失败了?我知道这与在第 2 行将 foo 设置为 this
有关,而不仅仅是使用 this
,但为什么会出现问题?
var foo = function () {
self = this;
self.boo = function () {
console.log("boo");
}
}
var bar = new foo().boo();
var baz = new foo().boo();
如果我设置self
等于this
,它通过了,为什么会这样?
问题出在下一行
foo = this;
上面一行遗漏了变量声明。所以 foo
将引用全局变量 foo
这是一个 constructor.When 你在该行第一次执行时调用该函数并且 foo
更改为 this
(实例foo
)
在第二种情况下,代码还创建了一个全局变量 self
,它将等于 this
。但在那种情况下,它不会更改构造函数,因为名称不同。
解决方法是使用let
(或const/var
)使foo
成为局部变量。
var foo = function () {
let foo = this;
foo.boo = function () {
console.log("boo");
}
}
var bar = new foo().boo();
var baz = new foo().boo();
虽然您已经得到了答案,但您可能不会明确设置this
。如果您在 foo
函数中 console.log
,它将记录一个 object
。相反,您可以 return
一个 object
,它将具有所有内部功能
var foo = function() {
return {
testVal: 5,
boo: function() {
console.log("boo ", this.testVal);
}
}
}
var bar = new foo().boo();
var baz = new foo().boo();