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!

Community
  • 1
  • 1
user1101431
  • 47
  • 1
  • 8
  • 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. – PhillipKregg Sep 18 '12 at 03: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. – user1101431 Sep 18 '12 at 03:35
  • Sounds like you may want to look into a databinding framework for JavaScript: KnockoutJS, AngularJS, and Ember are all good. – PhillipKregg Sep 18 '12 at 04:21
  • The question title is totally misleading. – ceving Oct 09 '15 at 08:24

1 Answers1

1

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

Community
  • 1
  • 1
Jordão
  • 55,340
  • 13
  • 112
  • 144