JavaScript - 为什么我们必须 return 来自自调用函数的函数?
JavaScript - why must we return a function from a self-invoking function?
此代码存在运行时错误:
var person = (function(){
var Person = {
init: function() {
},
};
return new Person();
/*return function(){
new Person();
}*/
})();
console.log(person);
它说我必须 return 函数而不是普通对象。
为什么我不能 return 来自 self-invoking/anonymous 外部函数的对象?为什么我必须 return 一个函数?
同样,这个修改后的代码也给了我一个类似的错误:
var person = function(){
var Person = {
init: function() {
},
};
return new Person();
/*return function(){
new Person();
}*/
};
console.log(person());
Why is that I can't return an object from the self-invoking/anonymous outer function?
你 可以 return 一个对象,这不是你的代码有什么问题。
您的代码的问题在于 Person
是一个对象,而不是一个函数。调用 new Person()
无效。
var person = (function () {
return {
name: 'bob'
};
}());
console.log(person.name);
这里的问题是你把Person
声明为一个对象,你不能对一个对象使用new Person()
。要在 ES5 中创建 "class",您需要创建一个函数。
var person = (function(){
var Person = function() {
this.init = function() {
console.log('Initing!')
}
};
return new Person();
})();
console.log(person);
Why must I return a function?
因为 new
运算符创建了一个 Javascript 对象的实例 。要创建它,它需要一个 构造函数 。这就是您要询问的功能。
这几乎就像调用一个典型的函数
function add() {
return 1 + 1;
}
add();
// 2
但是当您使用 new
运算符调用它时,您会创建此函数对象的一个新实例(函数也是对象,在 Javascript 中)。
function myAdder() {
this.a = 1;
this.b = 1;
this.add = function () {
return this.a + this.b;
};
return this;
}
myObject = new myAdder();
myObject.b = 2;
myObject.add();
// 3
而 myAdder
的对象表示法将是:
function myAdder() {
return {
a: 1,
b: 1,
add: function () {
return this.a + this.b;
}
};
}
备选方案 是以下对象构造函数:
var person = function(){
var Person = function(){
this.init = function() {
};
};
return new Person();
};
您可以阅读有关面向对象编程的更多信息here。
此代码存在运行时错误:
var person = (function(){
var Person = {
init: function() {
},
};
return new Person();
/*return function(){
new Person();
}*/
})();
console.log(person);
它说我必须 return 函数而不是普通对象。
为什么我不能 return 来自 self-invoking/anonymous 外部函数的对象?为什么我必须 return 一个函数?
同样,这个修改后的代码也给了我一个类似的错误:
var person = function(){
var Person = {
init: function() {
},
};
return new Person();
/*return function(){
new Person();
}*/
};
console.log(person());
Why is that I can't return an object from the self-invoking/anonymous outer function?
你 可以 return 一个对象,这不是你的代码有什么问题。
您的代码的问题在于 Person
是一个对象,而不是一个函数。调用 new Person()
无效。
var person = (function () {
return {
name: 'bob'
};
}());
console.log(person.name);
这里的问题是你把Person
声明为一个对象,你不能对一个对象使用new Person()
。要在 ES5 中创建 "class",您需要创建一个函数。
var person = (function(){
var Person = function() {
this.init = function() {
console.log('Initing!')
}
};
return new Person();
})();
console.log(person);
Why must I return a function?
因为 new
运算符创建了一个 Javascript 对象的实例 。要创建它,它需要一个 构造函数 。这就是您要询问的功能。
这几乎就像调用一个典型的函数
function add() {
return 1 + 1;
}
add();
// 2
但是当您使用 new
运算符调用它时,您会创建此函数对象的一个新实例(函数也是对象,在 Javascript 中)。
function myAdder() {
this.a = 1;
this.b = 1;
this.add = function () {
return this.a + this.b;
};
return this;
}
myObject = new myAdder();
myObject.b = 2;
myObject.add();
// 3
而 myAdder
的对象表示法将是:
function myAdder() {
return {
a: 1,
b: 1,
add: function () {
return this.a + this.b;
}
};
}
备选方案 是以下对象构造函数:
var person = function(){
var Person = function(){
this.init = function() {
};
};
return new Person();
};
您可以阅读有关面向对象编程的更多信息here。