使用新函数时从javascript函数返回
在java脚本中,我遇到了一个有趣的情况,我无法理解: 考虑以下功能:使用新函数时从javascript函数返回,javascript,function,object,constructor,Javascript,Function,Object,Constructor,在java脚本中,我遇到了一个有趣的情况,我无法理解: 考虑以下功能: T = function(){ this.help = 'foo'; var n = function(){}; return 'test'; } 写入t=newt()时,t是实例对象,返回参数被忽略 但是,在写作的时候, T = function(){ this.help = 'foo'; var n = function(){}; return n; } 写入t=newt()将导致t成为函数,而不是对象。因此,不
T = function(){
this.help = 'foo';
var n = function(){};
return 'test';
}
写入t=newt()时
,t
是实例对象,返回参数被忽略
但是,在写作的时候,
T = function(){
this.help = 'foo';
var n = function(){};
return n;
}
写入t=newt()
将导致t
成为函数,而不是对象。因此,不会忽略返回值,而是忽略此
部分(分配局部变量)
所以我有两个问题:
为什么?
还有其他情况会发生这种情况吗李>
当您从“构造函数”(使用new
调用的任何函数)返回对象时,该对象将成为返回值。忽略任何其他返回值,并返回新构造的对象
文本字符串不是对象,而是函数。当您从“构造函数”(使用new
调用的任何函数)返回对象时,该对象将成为返回值。忽略任何其他返回值,并返回新构造的对象
文字字符串不是对象,而是函数。来自:
构造函数返回的对象成为整个新表达式的结果。如果构造函数没有显式返回对象,则使用在步骤1中创建的对象。(通常构造函数不返回值,但如果要覆盖正常的对象创建过程,则可以选择返回值。)
简单地说,这就是JavaScript中构造函数调用的工作方式,即如果返回nothing或基元类型(如普通字符串
或数字
)或this,那么它们将被忽略,并且this
最终将被返回。但是,如果从构造函数返回对象或函数,则该对象将随new
一起返回
我从这里借用了以下代码,这在这里可能会有所帮助
// Undefined return value.
function A() {
return;
}
// Reference to instance.
function B() {
return (this);
}
// String return value.
function C() {
return ("string");
}
// Number retun value.
function D() {
return (123);
}
// New object return value.
function E() {
return ({
foo: "bar"
});
}
// New array return value.
function F() {
return (["foo", "bar"]);
}
// New instantiation return value.
function G() {
return (new A());
}
// Native "object" return value -- this one would be the same
// for Number, Boolean, and String.
function H() {
return (new Number(123));
}
// -------------------------------------------------- //
// -------------------------------------------------- //
// See what reference we have as a result of instantiation.
console.log(new A());
console.log(new B());
console.log(new C());
console.log(new D());
console.log(new E());
console.log(new F());
console.log(new G());
console.log(new H());
这是回报
A {}
B {}
C {}
D {}
Object { foo="bar"}
["foo", "bar"]
A {}
Number {}
发件人:
构造函数返回的对象成为整个新表达式的结果。如果构造函数没有显式返回对象,则使用在步骤1中创建的对象。(通常构造函数不返回值,但如果要覆盖正常的对象创建过程,则可以选择返回值。)
简单地说,这就是JavaScript中构造函数调用的工作方式,即如果返回nothing或基元类型(如普通字符串
或数字
)或this,那么它们将被忽略,并且this
最终将被返回。但是,如果从构造函数返回对象或函数,则该对象将随new
一起返回
我从这里借用了以下代码,这在这里可能会有所帮助
// Undefined return value.
function A() {
return;
}
// Reference to instance.
function B() {
return (this);
}
// String return value.
function C() {
return ("string");
}
// Number retun value.
function D() {
return (123);
}
// New object return value.
function E() {
return ({
foo: "bar"
});
}
// New array return value.
function F() {
return (["foo", "bar"]);
}
// New instantiation return value.
function G() {
return (new A());
}
// Native "object" return value -- this one would be the same
// for Number, Boolean, and String.
function H() {
return (new Number(123));
}
// -------------------------------------------------- //
// -------------------------------------------------- //
// See what reference we have as a result of instantiation.
console.log(new A());
console.log(new B());
console.log(new C());
console.log(new D());
console.log(new E());
console.log(new F());
console.log(new G());
console.log(new H());
这是回报
A {}
B {}
C {}
D {}
Object { foo="bar"}
["foo", "bar"]
A {}
Number {}