如何分配给函数的成员?

发布于 2025-01-01 16:03:54 字数 567 浏览 1 评论 0原文

由于函数是第一类对象,因此应该可以分配给它们的成员。

我认为 arguments.callee 这样做是对的吗?

还有其他方法可以设置这些字段吗?

在第一种情况下如何设置field

function something1() {
    arguments.callee.field = 12;
} 

alert(something1.field); // will show undefined
something1();
alert(something1.filed); // will show 12

something2 = function() {
    arguments.callee.field = 12;
};

alert(something2.field); // will show undefined
something2();
alert(something2.field); // will show 12

更新1

我的意思是如何在函数运行时从函数内部访问成员。

Since functions are first class objects, it should be possible to assign to the members of them.

Am I right thinking that arguments.callee does this?

Are there any other ways to set these fields?

How is it possible to set field in first case?

function something1() {
    arguments.callee.field = 12;
} 

alert(something1.field); // will show undefined
something1();
alert(something1.filed); // will show 12

something2 = function() {
    arguments.callee.field = 12;
};

alert(something2.field); // will show undefined
something2();
alert(something2.field); // will show 12

UPDATE 1

I mean how to access members from within function when it runs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

可是我不能没有你 2025-01-08 16:03:54

您不需要使用 arguments.callee 来引用具有名称的函数;您可以只使用名称。当您使用语法声明函数时,自然会出现这种情况

function name(...) { ... }

;但即使在函数表达式中,您也可以提供临时名称:

(function temp_name(...) { ... })(arg);

因此,如果您想从函数内部设置属性,您可以编写:

function something1() {
    something1.field = 12;
} 

alert(something1.field); // will show undefined
something1();
alert(something1.field); // will show 12

something2 = function something2() { // note the second "something2"
    something2.field = 12;
};

alert(something2.field); // will show undefined
something2();
alert(something2.field); // will show 12

You don't need to use arguments.callee to refer to a function that has a name; you can just use the name. This is naturally the case when you declare the function using the

function name(...) { ... }

syntax; but even in a function expression, you're allowed to supply a temporary name:

(function temp_name(...) { ... })(arg);

So, if you want to set the properties from inside the function, you can write:

function something1() {
    something1.field = 12;
} 

alert(something1.field); // will show undefined
something1();
alert(something1.field); // will show 12

something2 = function something2() { // note the second "something2"
    something2.field = 12;
};

alert(something2.field); // will show undefined
something2();
alert(something2.field); // will show 12
恏ㄋ傷疤忘ㄋ疼 2025-01-08 16:03:54

我将这样做:

function something1() {
    this.field = 12;
} 

var test = new something1();

alert(test.field); // alerts "12"
test.field = 42;
alert(test.field); // alerts "42"

如果您要将其视为一个类,则需要先创建它的一个新实例,然后才能访问其字段。

JSFiddle

Here's how I would do it:

function something1() {
    this.field = 12;
} 

var test = new something1();

alert(test.field); // alerts "12"
test.field = 42;
alert(test.field); // alerts "42"

If you're going to treat it like a class, you need to create a new instance of it before you can access its fields.

JSFiddle

画▽骨i 2025-01-08 16:03:54

我认为arguments.callee这样做是对的吗?

是的,确实如此,但现在他们弃用了它。

还有其他方法来设置这些字段吗?

替换callee的官方方法是使用显式函数名称,如funcName.propertyName=...。然而,这并不总是方便,例如对于动态生成的函数。引用 John Resig 的话,我们会错过争论.callee,它对于特定任务非常有用。

Am I right thinking that arguments.callee does this?

Yes it did, but now they deprecated it.

Are there any other ways to set these fields?

The official way to replace callee is to use an explicit function name, as in funcName.propertyName=.... This is however not always convenient, for example, with dynamically generated functions. To quote John Resig, we're going to miss arguments.callee, it was quite useful for specific tasks.

酷到爆炸 2025-01-08 16:03:54

简单

function something1() {};
something1.Property = "Foo";

您可以像普通对象一样直接将属性分配给任何函数。如果说在 OOP 语言中创建静态属性和方法。

编辑

函数内部相同

function something1() {
     something1.AnotherProp = "Bar";
};
something1.Property = "Foo";

Simple

function something1() {};
something1.Property = "Foo";

You can directly assign the properties to any function just like a normal object. If to say in OOP language create static properties and methods.

Edit

The same inside the function

function something1() {
     something1.AnotherProp = "Bar";
};
something1.Property = "Foo";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文