0

How do I do the following whilst protecting the state of b?

var a = function(o){
  this.o = o;
  this.o.one = 'three';
}
var b = {'one':'two'};
var c = new a(b);

console.log(b.one);  //three

I realize that this works...

var a = function(o){
  this.o = {};
  this.o.one = o.one;
  this.o.one = 'three';
}
...

but what if I would like to 'import' the whole object?

EDIT

This is answered here -> JavaScript: How to pass object by value?

Thanks everyone!

4
  • What exactly are you trying to do? You could use a closure to "protect the state". Is your goal to create a class-like structure? If so, you can create a constructor function. More details about what you are trying to accomplish would help. Commented Sep 18, 2012 at 3:25
  • Template objects that contain html and css shortcode anchors which are combined to create differently styled things to present to the client, but my template objects kept getting altered. Commented Sep 18, 2012 at 3:35
  • Sounds like you may want to look into a databinding framework for JavaScript: KnockoutJS, AngularJS, and Ember are all good. Commented Sep 18, 2012 at 4:21
  • The question title is totally misleading. Commented Oct 9, 2015 at 8:24

1 Answer 1

1

You need to copy b. Look here for the idea and some caveats.

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.