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

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

问题描述

事实证明,由于存在与我的关键字相关的许多其他问题,因此很难找到合适的答案,所以我会在这里提问.

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

正如我们所知,javascript 中的函数是对象,它们有自己的属性和方法(更准确地说,函数实例,继承自 Function.prototype).

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);
    }
}

当使用 Firebug 的 DOM 资源管理器检查时,该属性按预期定义.但是,由于我不认为自己是 javascript 专家,因此我有以下问题:

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. 这种方法是否可以被视为正确"且符合标准?它可以在 Firefox 中运行,但有很多东西可以在网络浏览器中按预期运行,而且绝不是标准.
  2. 这种通过向对象添加新属性来改变对象的做法是一种好的做法吗?

推荐答案

对你的问题给出一个非常有意义的答案有点困难,因为你有点说这是我的解决方案,可以吗?"没有解释您要解决的问题(您甚至明确表示您不会解释为什么").您的代码看起来是可以运行的有效 JavaScript,但它看起来也不是最佳的做事方式.

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:

这种方法可以被认为是正确的"并且符合标准吗?它可以在 Firefox 中运行,但有很多东西可以在 Web 浏览器中按预期运行,而且绝不是标准.

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.

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

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.

话虽如此,对我来说,将属性添加到 myMethod 函数并没有真正意义,将其他属性添加到您的 something 会更常见对象(如果调用正确,您的 myMethod 函数将可以通过 this 关键字访问 something 的其他属性).

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.)

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

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.

您可能会从 MDN 上的一些文章中受益:

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

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

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