简体   繁体   English

将属性添加到JavaScript函数是否可以接受?

[英]Is it acceptable to add an attribute to a JavaScript function?

Is it acceptable to add an attribute or value to a JavaScript function? 向JavaScript函数添加属性或值是否可以接受?

Example: 例:

var f = 1;

function foo (param) {
    f++;
}

var fooFunc = foo;

fooFunc.dummy = f;

console.log('fooFunc: ' + fooFunc);
console.log('fooFunc.dummy: ' + fooFunc.dummy);

The above example creates a function ( foo ), then assigns it to a new variable ( fooFunc ) and then adds a dummy attribute to fooFunc . 上面的示例创建一个函数( foo ),然后将其分配给一个新变量( fooFunc ),然后将一个dummy属性添加到fooFunc

When run, this example prints the text of the function first, and then it prints the expected value ( 1 in this case). 运行时,此示例首先打印函数的文本,然后打印预期值(在本例中为1 )。 When printing the function, it doesn't show any indication of the dummy value: 打印该功能时,它不会显示dummy值的任何指示:

fooFunc: function foo(param) {
    f++;
}
fooFunc.dummy: 1 

JsFiddle here - open the browser's JavaScript console to see the log messages: http://jsfiddle.net/nwinkler/BwvLf/ JsFiddle在这里 - 打开浏览器的JavaScript控制台以查看日志消息: http//jsfiddle.net/nwinkler/BwvLf/

Why does this work? 为什么这样做? And where is the dummy attribute stored, and why isn't it printed when I log the function? 存储dummy属性的位置,以及在记录函数时为什么不打印?

Lastly, even if this works, is it a good idea (or an acceptable practice) to use this? 最后,即使这有效,使用它是一个好主意(或可接受的做法)? I don't want to start an open ended discussion on this, but rather see if there's documented uses of this, or people discouraging this in JavaScript coding guidelines. 我不想就此开始一个开放式的讨论,而是看看是否有记录的使用,或者人们在JavaScript编码指南中劝阻这一点。

Everything except primitives ( null, undefined, number, string, boolean ) in JavaScript are objects. JavaScript中除了原语(null,undefined,number,string,boolean)之外的所有东西都是对象。 So functions are basically objects. 所以函数基本上是对象。

Objects in JavaScript can have properties and methods, hence functions too. JavaScript中的对象可以具有属性和方法,因此也具有功能。

all functions inherit from Function.prototype and has certain properties ( name, length ) and methods ( .call, .apply ) coming through this chain. 所有函数都继承自Function.prototype,并具有通过此链的某些属性(名称,长度)和方法(.call,.apply)。

It is sometimes very useful to keep properties attached to the function itself, like cache information, number of invocations etc. There is nothing wrong out in using it this way. 保持属性附加到函数本身有时非常有用,例如缓存信息,调用次数等。以这种方式使用它没有任何错误。

More details : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function 更多细节: https//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function

Let's have a look at ECMAScript documentation (Which is the standard JavaScript is based on). 我们来看看ECMAScript文档(这是标准的JavaScript所基于的)。 Here's the 3rd. 这是第3个。 version of it: 版本:

http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf

Go to chapter 15, Native ECMAScript Objects. 转到第15章,Native ECMAScript对象。 15.3 > Function objects. 15.3>功能对象。

There's a lot of interesting information there concerning your question, but the first thing worth noticing is that function is an object. 关于你的问题有很多有趣的信息,但值得注意的第一件事是函数是一个对象。 As an object, it has attributes (predefined and that you can assign yourself). 作为对象,它具有属性(预定义并且您可以自己分配)。 For example, try: 例如,尝试:

console.log('fooFunc.name: ' + fooFunc.name);

It should display "foo" in your case. 它应该在你的情况下显示“foo”。 Since it's documented quite well, you can use it as a standard way, though it is not so well-spread and may seem a bit unusual. 由于它的记录非常好,你可以将它作为标准方式使用,虽然它不是那么好传播,看起来有点不寻常。

Hope this helps. 希望这可以帮助。

It is normal object behavior, whether "acceptable" or not. 这是正常的对象行为,无论是否“可接受”。

By using the function keyword you are actually calling the native predefined Function() constructor. 通过使用function关键字,您实际上调用了本机预定义的Function()构造函数。 Like any object constructor it returns an object after building it. 像任何对象构造函数一样,它在构建对象后返回一个对象。 Like any object, the returned object can have properties, including other functions as method properties. 与任何对象一样,返回的对象可以具有属性,包括其他函数作为方法属性。

var adder = function(a, b){return a+b};
adder.subtracter = function(a, b){return a-b};
console.log(adder(1,2)); // 3
console.log(adder.subtracter(1,2)); // -1

TIP: if you want to see the adder object and its subtracter method, switch to DOM view from Console view after running the above code in console and then search for "adder". 提示:如果要查看加法器对象及其减法器方法,请在控制台中运行上述代码后从控制台视图切换到DOM视图,然后搜索“加法器”。 You'll see the object there, and then you can collapse to see what it's made from, including a subtracter object. 你会看到那里的物体,然后你可以折叠以查看它的来源,包括一个减法器对象。

Of course, a function object is a special native object, which makes it possible to make calls like this: adder() and actually run some code. 当然,函数对象是一个特殊的本机对象,它可以像这样进行调用:adder()并实际运行一些代码。 The fact that a function object is harder to inspect for custom attached properties, combined with its native special object treats (read built-in restrictive behavior), should give you a hint that, while it's possible, attaching custom properties is not the intended nor a good use of a function object. 函数对象更难以检查自定义附加属性的事实,结合其本机特殊对象处理(读取内置限制行为),应该给出一个提示,尽管可能附加自定义属性不是预期的属性很好地利用了一个功能对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM