将自定义属性添加到函数 [英] Adding custom properties to a function

查看:134
本文介绍了将自定义属性添加到函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



正如我们所知道的那样,函数在JavaScript中是对象,他们有自己的属性和方法(更正确地说,函数istances,继承自Function.prototype)。

我正在考虑为一个函数添加自定义属性(方法),让我们跳过为什么?部分并直接进入代码:

  var something = {
myMethod:function(){
if(something.myMethod.someProperty === undefined){
something.myMethod.someProperty =test;
}
console.log(something.myMethod);


当使用Firebug的DOM浏览器进行检查时,如预期。然而,由于我不认为自己是一个JavaScript专家,我有以下问题:


  1. 这种方法可以被认为是适当的符合标准?它适用于Firefox,但在浏览器中有许多事情按预期工作,并不是以任何方式标准。

  2. 这种改变对象的方法是通过向它们添加新属性?


解决方案

对您的问题给出一个非常有意义的答案有点困难,你有点说:这是我的解决方案,可以吗?而没有解释你试图解决什么问题(你甚至明确表示你不会解释为什么)。你的代码看起来是有效的JavaScript代码,但它看起来还不够理想的做事方式。



如果你解释你实际想要达到的目标可能会有更好的方法来构建代码。不过,我会给你一些答案:


这种方法可以被认为是合适的并符合标准吗?它适用于Firefox,但在网络浏览器中有许多事情按预期工作,并不是以任何方式标准。


函数是对象(如你所说),因此可以为它们添加属性。这不是一个真正的标准问题,因为它是所有浏览器支持的JavaScript的核心部分。


这种类型的通过向他们添加新属性来改变对象是一种很好的做法吗?

这是你的对象,你可以添加你喜欢的任何属性。整个对象的要点是它们具有可以操纵的属性。我无法真正设想一种使用不涉及改变它们的对象的方法,包括添加,删除和更新属性和方法。



话虽如此,对我来说将属性添加到 myMethod 函数并没有什么意义,通常将其他属性添加到某物中 object(如果调用正确,您的 myMethod 函数可以通过> 来访问其他属性 this 关键字)。



如果您将函数用作构造函数方法添加到关联的原型并向每个实例添加(非方法)属性是有意义的,但是在适当的情况下,您可以采用其中一种方法或两种方法。 (注意一个方法本质上只是引用函数的一个属性。)

您显示的特定代码不会添加属性,它会测试是否 someProperty 属性已经存在,如果是,则为其指定一个新值。



您可以通过阅读MDN等文章获益:


Searching for appropriate answer proved difficult because of the existence of many other problems related to my keywords, so I'll ask this here.

As we know, functions in javascript are objects and they have their own properties and methods (more properly, function istances, inherited from Function.prototype).

I was considering adding custom properties for one function (method), let's skip the "why?" part and go straight to the code:

var something = {
    myMethod: function () {
        if (something.myMethod.someProperty === undefined) {
            something.myMethod.someProperty = "test";
        }
        console.log(something.myMethod);
    }
}

When inspected with Firebug's DOM explorer, the property is defined as expected. However, as I don't consider myself a javascript expert, I have the following questions:

  1. Can this method be considered "proper" and standards compliant? It works in Firefox but there are many things working as expected in web browsers and aren't by any means standards.
  2. Is this kind of altering objects by adding new properties to them a good practice?

解决方案

It's a little bit difficult to give a very meaningful answer to your question, because you've sort of said "Here is my solution, is it OK?" without explaining what problem you are trying to solve (you even said explicitly that you are not going to explain the "why"). Your code looks to be valid JavaScript that will run, but it also looks like a less than optimal way of doing things.

If you explain what you actually want to achieve you may get some good suggestions on better ways to structure your code. Still, I'll give you some kind of answer:

Can this method be considered "proper" and standards compliant? It works in Firefox but there are many things working as expected in web browsers and aren't by any means standards.

Functions are objects (as you've said), and thus it is possible to add properties to them. This isn't really a standards issue as such in that it is a core part of JavaScript that all browsers support.

Is this kind of altering objects by adding new properties to them a good practice?

It's your object, you can add whatever properties you like. The whole point of objects is that they have properties that you can manipulate. I can't really envisage a way of using objects that doesn't involve altering them, including adding, deleting and updating properties and methods.

Having said that, to me it doesn't really make sense to add properties to the myMethod function, it would be more usual to add other properties to your something object (your myMethod function would, if called correctly, have access to the other properties of something via the this keyword).

If you are using a function as a constructor it typically makes sense to add methods to the associated prototype and add (non-method) properties to each instance, but you can do either or both the other way when appropriate. (Noting that a "method" is essentially just a property that happens to reference a function.)

The specific code you have shown doesn't add properties, it tests whether the someProperty property already exists and if so assigns it a new value.

You might benefit from reading some articles such as these at MDN:

这篇关于将自定义属性添加到函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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