1

I just noticed IDEA / JSHint telling me that an object literal which contains a property named "create" overrides a method in Object.

The literal is essentially:

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

And (obviously?) Object has a create method defined in EcmaScript5.js

/**
@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.

4
  • Reserved word in JS: [link] w3schools.com/js/js_reserved.asp Should not be a problem
    – Legends
    Mar 31, 2015 at 23:47
  • seriously, downvote? its just a simple question.
    – 4m1r
    Mar 31, 2015 at 23:47
  • appreciated @Legends. Thanks for not being a troll. =)
    – 4m1r
    Mar 31, 2015 at 23:50
  • my guess is that since its not a reserved word, there's nothing wrong with intentionally or unintentionally overriding it.
    – 4m1r
    Apr 1, 2015 at 0:12

4 Answers 4

1

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.

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

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:

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.

1
  • This is the answer I was looking for. Thanks for your thoughtful response, James.
    – 4m1r
    Apr 1, 2015 at 16:38
0

It's not reserved, but it is in use (defined) on the Object object.

Since a literal object becomes an object too, the custom create() method would override the existing one.

Just use a different name for your method.

0

It's not reserved, it's a valid identifier, but, as you said, you're overriding the Object create method on your literal. It will only affect if you try to use create on your literal.

myModule.email.create(); // Will fail
myModule.email.create; // "me@me.com"
2
  • so, if create is a function, and you do email.create() i guess the function will work, but you loose the ability to actually create an object from that object.
    – 4m1r
    Apr 1, 2015 at 0:07
  • best answer. thanks everybody for entertaining the question.
    – 4m1r
    Apr 1, 2015 at 0:28
0

Just so you know when you create a global variable or method in JavaScript (in a browser) they become properties of the window object.

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.

<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. So the first click opens a new window and then when you click the second button the behavior of window.open is changed. 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. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.