0

Document.prototype.greenify = function(){
    return {
        style : function(){
            return this.color = "green";
        }
    }
};
document.getElementsByTagName("H1")[0].greenify();
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<h1>TEST</h1>
<script type="text/javascript" src="main.js"></script>
</body>
</html>

Hey out there,

I wanna build a 'dot function'. My function for trial should 'greenify' my element. I already tried to add the function to Window- object but I came to the same result. So, now my question... What I did wrong or did I forget something? I'm thankfully for each answer I receive :)

4
  • 2
    By "dot function" do you mean a method? You need to add it to the prototype of the objects you'll be invoking it on, but adding to someone else's prototype (even the browser's) is a Bad Idea. Commented Mar 12, 2015 at 15:27
  • 3
    ... please don't do this. Add a function greenify(), which accepts a HTML element, and do that instead. Commented Mar 12, 2015 at 15:27
  • Despite this can be done, I totally agree with Matt. Of course it's matter of opinions, but take a look at this article: perfectionkills.com/whats-wrong-with-extending-the-dom .. I would rather recommend you to make your own little "object" to deal with such things instead. Commented Mar 12, 2015 at 15:29
  • @briosheje It's not a matter of opinion, it's actually dangerous on the grounds that a variable or library can be set up with a no-collision option, but that's not practical if you are extending someone's prototype. Depending on the implementation, you may also run into unusual prototypes around native objects. Commented Mar 12, 2015 at 15:30

1 Answer 1

5

h1 elements don't inherit from Document.prototype. They inherit from those:

  • HTMLHeadingElement.prototype
  • HTMLElement.prototype
  • Element.prototype
  • Node.prototype
  • EventTarget.prototype
  • Object.prototype

For example, you can add the method to HTMLElement.prototype:

HTMLElement.prototype.greenify = function(){
    this.style.color = "green";
};
document.getElementsByTagName("H1")[0].greenify();

HTMLElement.prototype.greenify = function(){
    this.style.color = "green";
};
document.getElementsByTagName("H1")[0].greenify();
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<h1>TEST</h1>
<script type="text/javascript" src="main.js"></script>
</body>
</html>

However, note that modifying objects you don't own is considered a bad practice.

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

2 Comments

Note that adding to a prototype not under your control, especially a native one, is bad practice and may cause collisions with other libraries. You should avoid it whenever possible. This answer is correct, however, so no dv.
Thank you very much I already tried it from the beginning of the week. :)

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.