Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jan 2013
    Location
    Sunnyvale, CA
    Posts
    144
    Thanks
    11
    Thanked 7 Times in 7 Posts

    Require syntax assistance

    I want to create a js object that receives an object as a sole argument and copies all of its porperties - both the keys and their values - to itself, kind of like:

    function empty(Parameter) {
    for (var key in Parameter){
    /*copy the key/value pair from Parameter to this.*/
    }
    }


    Basically, if Parameter.ID = 5, then empty will have the property this.ID = 5 in addition to all other Parameter properties... Basically a shallow copy. Parameter does not contain objects.

    I cannot seem to get the syntax right. Assistance appreciated.

    Thx
    Last edited by sbhmf; 06-01-2013 at 06:42 AM.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,469
    Thanks
    13
    Thanked 361 Times in 357 Posts
    with for…in you won’t get a shallow copy (it also checks the prototype chain, it does not loop over non-enumerable properties and it skips null/undefined values).

    the method of choice would be Object.getOwnPropertyNames(). and for setting the properties for the new object, you can use Object.defineProperty() and Object.getOwnPropertyDescriptor().

    I would recommend not to attach them to this, unless your function is an object’s method (because simply calling the function would attach the properties to the window object).
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,387
    Thanks
    32
    Thanked 288 Times in 282 Posts
    Quote Originally Posted by sbhmf View Post
    I want to create a js object [...]

    [...] empty will have the property this.ID = 5 in addition to all other Parameter properties... Basically a shallow copy. Parameter does not contain objects.

    I cannot seem to get the syntax right. Assistance appreciated.
    Where's the object you plan on copying the properties to? empty is a function, not an object, so if you're trying to assign empty properties (like ID), it's no surprise that your code isn't working.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #4
    Regular Coder
    Join Date
    Jan 2013
    Location
    Sunnyvale, CA
    Posts
    144
    Thanks
    11
    Thanked 7 Times in 7 Posts
    The function is itself the object definition, so 'empty' in this context is actually a type ( in the sense that var x = new empty(); ). The objective was for 'empty' to incorporate Parameter's properties into itself, so that they may be referenced when the scope has a different context (i.e. empty.ID, where Parameter is no longer accessible).

    I was trying to develop an idea for a solution to a problem, and have since scrapped this option for a cleaner one.

    Many thanks for your responses.

  • #5
    Regular Coder
    Join Date
    Mar 2008
    Location
    London
    Posts
    152
    Thanks
    4
    Thanked 42 Times in 42 Posts
    Something like this ?

    Code:
    function Person(n, a, g) {
      this.name = n;
      this.age = a;
      this.gender = g;
      this.toString = function() {
        return this.name + ', ' + this.age + ', ' + this.gender;
      }
    }
    
    function Empty(obj) {
      var prop = Object.getOwnPropertyNames(obj);
      for(var i = 0; i < prop.length; i++) {
        this[prop[i]] = obj[prop[i]];
      }
    }
    
    var p1 = new Person('Bob', 25, 'Male');
    alert(p1);
    var c1 = new Empty(p1);
    alert(c1);

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,469
    Thanks
    13
    Thanked 361 Times in 357 Posts
    what if the Person object looks like this:
    PHP Code:
    function Person(nag) {
      
    this.name n;
      
    this.age a;
      
    this.gender g;
    }
    Person.prototype.toString = function() {
      return 
    this.name ', ' this.age ', ' this.gender;
    }; 
    besides that, the Empty function still has a dangerous feat lurking around:
    PHP Code:
    var p1 = new Person('Bob'25'Male');
    var 
    c1 = Empty(p1); // note the missing of "new" 
    if Empty() is used as a single function, it would be better to pass both objects in
    PHP Code:
    // - does not copy prototype
    // - does not retain property definitions
    function copy(sourcetarget) {
      var 
    prop Object.getOwnPropertyNames(source);
      for(var 
    0prop.lengthli++) {
        
    target[prop[i]] = source[prop[i]];
      }

    Last edited by Dormilich; 06-03-2013 at 04:16 PM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •