0
<!DOCTYPE html>
<html>
<body>

 <p id="demo"></p>

  <script>
  Date.prototype.name="not good";

 var d=new Date();

    document.getElementById("demo").innerHTML =d.name;

   </script>

   </body>
   </html>

Result-

not good

In the above example the name field is being added to the Data object using the prototype functionality.

What could be the drawbacks of doing this? Are the changes made here will reflect permanently?

According to w3scholls I got this note- Only modify your own prototypes. Never modify the prototypes of standard JavaScript objects.

But sometimes isn't it convenient?

10
  • it can be handy, but it slows down code and can cause conflicts with library code that wasn't written with the modification in mind. Commented Jul 7, 2015 at 3:01
  • @dandavis - I'm pretty sure it won't slow down code. Commented Jul 7, 2015 at 3:03
  • While working with your own code, you may have a handle on what you're using where, but in large codebases overwriting the standard objects can be detrimental to other peoples' code. If you're wanting to expand/change the base javascript prototypes, it's a better idea to create a wrapper around them and alter the wrapper object's prototype Commented Jul 7, 2015 at 3:07
  • @techfoobar: Date.prototype might be exempt (not sure), but modifying data protos will de-optimize V8, probably others. Commented Jul 7, 2015 at 3:08
  • 1
    For as long as that page is loaded. It's a modification for the duration of that page being loaded only. It won't affect other pages that don't have the same script. Every page load starts with a new, fresh unmodified environment. Commented Jul 7, 2015 at 4:51

1 Answer 1

3

Except for polyfills which implement standard-defined behavior in older implementations, it is generally not considered a good practice to modify the implementation of built-in objects. These are some of the reasons:

  1. A standard object is a public namespace. The standards may evolve over time and add new methods/properties which could conflict with the properties you add. Better to avoid that possibility and avoid code that could conflict with future browsers.

  2. If multiple pieces of code being used in a project are all doing this (imagine a project that pulls in 15 third party libraries to do its job), then there is a chance for conflict among the modifications (since they aren't defined by any standards).

  3. You may be modifying the behavior of a standard object in a way that could break some existing code. For example, adding an enumerable property or method to some objects can mess up existing code that iterates existing properties. If you were going to add something, make sure you add it with Object.defineProperty() so that it is not enumerable.

  4. There's pretty much always another way to accomplish what you want to do that doesn't have these risks and works just as well. For example, jQuery chooses to create a container wrapper (it's own object) that contains references to DOM objects and is the container object for its own methods.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.