5

I have created a custom object and added a method to it which is called changeColor with an attribute. The idea is when I attach this method to any DOM element that elements all content color must change.

Here is my started code:

function CustomColor() {

}

CustomColor.prototype.changeColor = function(color){
   //here what I have to specify.

}

This is pretty basic but I am new to JavaScript. Thanks.

7
  • 3
    I don't understand "I will attach this method to any DOM element that elements all content color will change". Which DOM elements' color do you want to change? Extending the DOM with your own methods is a bad idea, but creating wrappers is no problem. Commented Jan 26, 2013 at 18:24
  • 2
    Wells thanks, but what is the question. Commented Jan 26, 2013 at 18:25
  • Ok, in my web page one paragraph is there. And its id is myParagraph. So when I will attach to the changeColor method to the paragraph, it will change the specified color. Commented Jan 26, 2013 at 18:29
  • That would be pretty useless, if you change all the color attributes then most elements will be nothing but a unicoloured block. In any case, changing an attribute is so simple in the first place that making a custom method for it makes little sense, you can't make anything substantially simpler than element.style.color="red". Commented Jan 26, 2013 at 18:31
  • Just change the color! It's pointless to attach the suggested custom function/method to another DOM element that changes the colour like this, unless of course you're simply looking at the principals behind it. Commented Jan 26, 2013 at 18:33

3 Answers 3

5

use CustomColor like this:

function CustomColor(element) {
    this.element = element;
}
CustomColor.prototype.changeColor = function (color) {
    this.element.style.color = color;
}

new instance of CustomColor:

var element = new CustomColor(document.body);
element.changeColor('red');

you can also extend the actual dom element without using any extra classes like this:

Element.prototype.changeColor = function (color) {
    this.style.color = color;
};

and use it like this:

document.body.changeColor('red')
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks my friend for your help. I applied your logic in my code. It is working just fine.
2

I suppose this is what you're looking after:

var elem = document.getElementById('some_element'),
    CustomColor = function (element) {
        this.htmlElement = element;
    };
CustomColor.prototype.changeColor = function (color) {
    this.htmlElement.style.color = color;
    return;
};
elem.customColor = new CustomColor(elem);

Using attached property:

elem.customColor.changeColor('#00f');

Only way to get a reference to the hosting HTML element is to pass it as an argument to the constructor. However, in changeColor you can't refer any "private" variable of CustomColor (including its arguments), hence you'll need to create a "public" property for all those properties you want to use in changeColor. More info at MDN

A live demo at jsFiddle.

The code above creates a custom property only to a specific HTML element. It's not recommended to create custom properties to the prototype of a DOM element, since those are not supported in all browsers.

Comments

0

You must use the name local inside a function or object.

let ob = {
    theif : 'Davron',
    height : 1.7,
    color : {
        hair : 'qora',
        esee : 'white',
    },

    methodName: function () {
        return 'This is medthod';
    }
}

console.log(ob.methodName());

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.