我能否知道“new Func()”是否属于或“Func()”或叫做? [英] Can I know if "new Func()" or "Func()" is called?

查看:72
本文介绍了我能否知道“new Func()”是否属于或“Func()”或叫做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如我写了一个函数:

函数Func()

{

//做点什么

}


我们可以这样称呼:

var obj = new Func(); //将其称为构造函数



var result = Func(); //将其称为函数


但是,我可以知道是否new Func()或Func()或被叫?


#对不起我的英语不太好

For example I wrote a function:
function Func()
{
// do something
}

we can call it like:
var obj = new Func(); // call it as a constructor
or
var result = Func(); // call it as a function

but, can i know if "new Func()" or "Func()" is called?

# sorry my english is not so good

推荐答案


jh*****@gmail.com D?D°D?D ??? D°D2:

jh*****@gmail.com D?D°D?D???D°D2:

例如我写了一个函数:

函数Func()

{

//做点什么

}


我们可以这样称呼:

var obj = new Func(); //将其称为构造函数



var result = Func(); //将其称为函数


但是,我可以知道是否new Func()或Func()或被叫?


#对不起我的英语不太好
For example I wrote a function:
function Func()
{
// do something
}

we can call it like:
var obj = new Func(); // call it as a constructor
or
var result = Func(); // call it as a function

but, can i know if "new Func()" or "Func()" is called?

# sorry my english is not so good



typeof(obj)总是对象, typeof(result)取决于结果值

Func()返回。

对不起,我的英语也有很多不足之处。 :)

typeof(obj) always is "object", typeof(result) depends on result value
that Func() returns.
Sorry, my English also leaves much to be desired. :)


我怎么知道new Func()是否属于或Func()或被调用?


如果函数被调用为构造函数,它将在新创建的对象的范围内运行,所以[this]指向所述对象。如果

它被称为函数,对于顶级函数[this]设置为

window对象(谈论在HTML中执行的常规脚本

文件)因此,在最原始的形式,支票将是:


函数Func(){

if(this == window) ){

//被称为函数

}

else {

//被称为构造函数

//或(注意!)作为对象的方法

}

}


那是'' s工作可靠只能解决顶级构造函数的问题

称为常规函数。对于更复杂的情况,你应该提前告诉你的构造函数,[这个]值将是

接受,比如你的构造函数中的内部变量。

How can I know if "new Func()" or "Func()" is called?

If a function is called as a constructor, it''s running in the scope of
the newly created object, so [this] is pointing to the said object. If
it''s called as a function, for top level functions [this] is set to
window object (talking about a regular script executed in a HTML
document) Thus in the most primitive form the check will be:

function Func() {
if (this == window) {
// called as function
}
else {
// called as constructor
// or (attention!) as method of an object
}
}

That''s work reliable only to sort out cases of top-level constructors
called as regular functions. For more sophisticated cases you should
instruct your constructor in advance which [this] values it will
accept, say via internal variable in your constructor.


VK写道:
VK wrote:

如何知道new Func()是什么?或Func()或被调用?


如果一个函数被调用为构造函数,它将在新创建的对象的

范围内运行,
How can I know if "new Func()" or "Func()" is called?

If a function is called as a constructor, it''s running in the
scope of the newly created object,



函数中的范围不受其调用方式的影响。

没有新创建的对象的范围这样的东西。 (除非

对象是函数)。

The scope within a function is not influenced by how it is called.
There is no such thing as "the scope of a newly created object" (unless
the object is a function).


所以[this]指向所述对象。
so [this] is pointing to the said object.



< snip>


- this - value不是函数范围的一个方面。 - 这个 -

值和适用范围是执行的特征

背景。


Richard。

<snip>

The - this - value is not an aspect of a function''s scope. The - this -
value and the applicable scope are characteristics of the execution
context.

Richard.


这篇关于我能否知道“new Func()”是否属于或“Func()”或叫做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆