简体   繁体   English

可以在JavaScript对象文字中使用“创建”作为属性名称吗?

[英]Is it okay to use “create” as a property name in a JavaScript object literal?

I just noticed IDEA / JSHint telling me that an object literal which contains a property named "create" overrides a method in Object. 我刚刚注意到IDEA / JSHint告诉我,包含名为“ create”的属性的对象文字覆盖了Object中的方法。

The literal is essentially: 字面本质上是:

module.exports = {email:{create:"me@me.com"}};

And (obviously?) Object has a create method defined in EcmaScript5.js 并且(显然吗?)对象在EcmaScript5.js中定义了一个create方法

/**
@param {Object} proto
@param {Object} [props]
@static
@return {Object}
*/
Object.create = function(proto,props) {};

Is this something that could cause an obscure problem down the line? 这是否可能会导致晦涩的问题? I'm guessing that this reserved method doesn't apply to literals, or objects which haven't been instantiated with a default constructor. 我猜想这个保留的方法不适用于文字或未使用默认构造函数实例化的对象。 Just curious. 只是好奇。

The existing answers are correct but missing some important detail. 现有的答案是正确的,但缺少一些重要的细节。 What you are doing is absolutely fine and will not cause errors in any JavaScript environment. 您正在做的事情绝对没问题,不会在任何JavaScript环境中引起错误。

The Object.create method that's been mentioned numerous times is static , which means is a property of the Object constructor itself, rather than its prototype. 多次提到的Object.create方法static ,这意味着是Object构造函数本身的属性,而不是其原型。 You are not overwriting it, or even shadowing it. 您没有覆盖它,甚至没有遮盖它。 It will still be accessible: 仍然可以访问:

var obj = { create: 'something' };
console.log(obj.create); // 'something'
console.log(Object.create); // function create() { [native code] }

I'm not sure why JSHint or any other static analysis tool would warn against the use of create as a property identifier, except perhaps for the reason that it could cause some potential confusion. 我不确定为什么JSHint或任何其他静态分析工具会警告不要将create用作属性标识符,除非可能是因为它可能引起一些潜在的混乱。

Even your concern about create being a reserved word in JavaScript is a non-issue because modern JavaScript environments allow the use of reserved words as property identifiers, and create is not a reserved word in the first place: 即使您担心将create用作JavaScript中的保留字也是没有问题的,因为现代JavaScript环境允许将保留字用作属性标识符,而create最初不是保留字:

var obj = {
  default: 1 // Reserved word as identifier
};

So in summary, you are safe to ignore the warning and don't worry about any potential side-effects your code might have had. 因此,总而言之,您可以放心地忽略该警告,并且不必担心您的代码可能具有的任何潜在副作用。

It's not reserved , but it is in use (defined) on the Object object. 不是保留的 ,但在Object对象上正在使用 (定义)。

Since a literal object becomes an object too, the custom create() method would override the existing one. 由于文字对象也成为对象,因此自定义create()方法将覆盖现有对象。

Just use a different name for your method. 只需为您的方法使用其他名称即可。

It's not reserved, it's a valid identifier, but, as you said, you're overriding the Object create method on your literal. 它不是保留的,它是一个有效的标识符,但是,正如您所说的,您将覆盖文字上的Object create方法。 It will only affect if you try to use create on your literal. 仅当您尝试在文字上使用create时,它才会影响。

myModule.email.create(); // Will fail
myModule.email.create; // "me@me.com"

Just so you know when you create a global variable or method in JavaScript (in a browser) they become properties of the window object. 就像您知道的那样,当您在JavaScript中(在浏览器中)创建全局变量或方法时,它们将成为窗口对象的属性。

It is absolutely fine to introduce or even change the method definitions if you have a good reason to do so. 如果有充分的理由介绍或更改方法定义,那是绝对好的。 Of course there are things that can go wrong if you change a method behavior. 当然,如果更改方法行为,某些事情可能会出错。 Here is an example - I overrode the window's open method. 这是一个示例-我覆盖了窗口的open方法。

<html lang="en">
<head>
<script>
function windowOpen() {
window.open("http://www.google.com")
}

function windoOpenChange() {
window.open = function () {
alert("I won't open the window!");
};
}
</script>
</head>
<body>
<button value="open window" onClick="windowOpen()">Go to Google</button>
<button value="open window" onClick="windoOpenChange()">Change the behavior of window open</button>
</body>
</html>

This is tested in FF. 这是在FF中测试的。 So the first click opens a new window and then when you click the second button the behavior of window.open is changed. 因此,第一次单击将打开一个新窗口,然后在单击第二个按钮时,window.open的行为将更改。 Now when you click the first button again it will no longer open the new window. 现在,当您再次单击第一个按钮时,它将不再打开新窗口。 So yes this could be seen as an undesirable side effect if this is not what you intended to do. 因此,是的,如果您不打算这样做,这可能被视为不良的副作用。

The ability of being able to introduce new methods from other objects is called Mixin . 能够从其他对象引入新方法的能力称为Mixin And of course based on your mixin logic any mixin could run into conflicts not just built in objects. 当然,根据您的混合逻辑,任何混合都可能会发生冲突,而不仅仅是内置对象。

The tools you are using are warning you against potential shadowing (unwanted override) of methods. 您使用的工具正在警告您避免方法的潜在阴影(不必要的覆盖)。

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

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