0

I am looking to set an attribute of an object dynamically. Look at the example below.

function testFunc(type, scope){
    this.scope = scope;
    this.scope.setAttribute(type, true);
    this.doSome = function(){return //Something;}
}

but I realized that setAttribute method is only available for DOM elements. Is there a way i can set an attribute dynamically to a js object ?

3
  • 4
    Are you looking for this.scope[type] = true? Commented Mar 16, 2013 at 8:27
  • Yes absolutely, just figured it out. Thanks. Commented Mar 16, 2013 at 8:27
  • possible duplicate of Dynamic object property name Commented Mar 16, 2013 at 9:28

1 Answer 1

2
function testFunc(type, scope){
    this.scope = scope;
    this.scope[type]= true;
    this.doSome = function(){return //Something;}
}

should do the trick.

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.