-1

I'm trying to build a contructor which accepts an object and sets it to itself.

For instance:

var Somecontructor = function(object){
    this = object;
}

Obviously, I cannot assign anything to "this" because it is immutable, but I'm trying to figure out a way to do this. Any ideas?

Edit: I don't know why this question deserves negative vote. All I want to do is, construct an object that creates properities using whatever that was passed as arguments. Sure, I can do this and I wanted to find the most efficient way to do it. If your answer doesn't work, please don't be a terrible human being by negative voting the question.

Thilak Rao
  • 1,841
  • 2
  • 19
  • 26
  • What is the purpose? – didierc Dec 15 '14 at 10:12
  • If you want to set it just to return is later..why not return it itself from the function? – Harsh Dec 15 '14 at 10:14
  • Could you give us more code, so we know what exactly you want to do with the object passed to constructor? – Bartek Andrzejczak Dec 15 '14 at 10:15
  • I want to pass an arbitrary object and the I want the properties of that object to be assigned to the newly created object. – Thilak Rao Dec 15 '14 at 10:19
  • Are you trying to clone the object? –  Dec 15 '14 at 10:20
  • Yes, I am trying to clone it. And whoever gave that -1, thank you so much! – Thilak Rao Dec 15 '14 at 10:20
  • @ThilakRao Try `JSON.parse(JSON.stringify(obj))`, instead of using a function. – thefourtheye Dec 15 '14 at 10:23
  • Didn't downvote myself, but I would have to say that the question is low quality on a number of levels. The expression "setting an object to itself" is somewhere between a tautology and a no-op. If you wanted to copy properties, you needed to say that. It's nothing against you that you don't know the word "clone", but if you had, everyone would have understood instantly what you wanted. In fact, in that case, you wouldn't even have to have posted the question, because you could search SO or the web for "JS object clone" and gotten like 1000 answers. –  Dec 15 '14 at 12:46
  • possible duplicate of [How do I clone a JavaScript class instance?](http://stackoverflow.com/questions/16024940/how-do-i-clone-a-javascript-class-instance) –  Dec 15 '14 at 12:52
  • See also http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-clone-an-object, which has like a million upvotes. –  Dec 15 '14 at 12:53
  • @torazaburo Bob, My exact use case for this need is a bit hard to explain, and didn't want to bore anyone with details. I'm trying to build a wrapper around XHR object, and add some helper methods that let me parse the JSON response in certain ways. I was planning on passing 2 - 3 properties via the object, and could have mostly done it using arguments, but just felt like doing it using an object instead. Performance is a non-goal in this context. Maybe the question was indeed low quality, or maybe I didn't articulate it well enough. Good to meet you here! – Thilak Rao Dec 15 '14 at 17:17

2 Answers2

1

You seem to be wanting to clone an object. For that, you need not use a constructor function.

var cloneObj = function(obj){
   return JSON.parse(JSON.stringify(obj));
}
console.log(cloneObj({
   property1: "One",
   property2: "Two",
}));
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
1

This will copy all of the properties of one object to the properties of a new object.

var SomeConstructor = function(obj) {
    for(key in obj) {
        this[key] = obj[key];
    }
}
Philip Adler
  • 2,111
  • 17
  • 25
  • Thanks! This helps. Your solution worked as desired, and I'm going to mark this as the answer. http://jsbin.com/hixaq/1/edit?js,console – Thilak Rao Dec 15 '14 at 11:53
  • @torazaburo; my bad, I started writing the answer before the clone edit appeared at my end, and so assumed OP wanted a shallow copy. On the other hand, if it works for the OP, then it works, so now I am not sure what I should do. I'd also like to commend you on making your critique completely impersonal. Exemplary commenting! – Philip Adler Dec 15 '14 at 21:33
  • This is a fine solution for copying simple objects. You should be aware that it just does a shallow copy, and also if prototypes are involved may not work exactly like you want. Note: you can also use `$.extend`, or `_.extend`, or your favorite library's equivalent to do approximately the same thing. –  Dec 16 '14 at 03:08
  • @PhilipAdler It's accepted and it works so I'd leave it there. –  Dec 16 '14 at 03:08