0

If I have an object :

 function myClass(id) {
   this.em = document.getElementById(id);
   this.html = function(data) {
     this.em.html = data;
   }
 }

Now I can :

  var em = new MyClass("id");
  em.html("NEW HTML HERE");

I need :

em.html = "NEW HTML HERE";

Is It possible?

1 Answer 1

3

In HTML5 you could define a set method on the html property (see defineProperty())

function myClass(id) {
    this.em = document.getElementById(id);    

    Object.defineProperty(this, 'html', {
        set: function(val) {
            this.em.html = val;
        }
    });
}

... but this will only work in the most modern browsers; IE8, Chrome 5, Firefox 4.

See a demo of the above working here; http://jsfiddle.net/sskKc/

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.