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.
Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22
  1. #16
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    The clearest explanation of prototype I have found is at

    http://yehudakatz.com/2011/08/12/und...in-javascript/

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  2. #17
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by Philip M View Post
    For the life of me I do not see the use of "two arrays with the same content. (Or, more correctly, two variables referencing the same array object.)".
    How can it make sense to lose/replace the original array? That was the point I was trying to make.
    if the array is a deck of cards in a game, and you want all players drawing from the same shuffled deck, it makes a lot of sense.

    another mutative example from a real project is var filename=pathRay.pop(), path=pathRay.join("/");

    another mutative example: gridRows.sort( sorter.textNoCase("lastName") ); to sort a grid before rendering it, uses should be intuitive.


    i myself used to use a lot of custom array prototypes like sum(), filtered(), etc. but [].map changed all that. I can now just use a tiny pure function method on an [] method to do all that stuff: r.reduce(F.sum), r.filter(Boolean), etc. i still use [].unique a lot, since it's slow to do with a filter method...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  3. #18
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by rnd me View Post
    if the array is a deck of cards in a game, and you want all players drawing from the same shuffled deck, it makes a lot of sense.
    Yes, but that is somewhat specialised. My customers have never asked for card games on their sites. But if they did I feel that the code I posted in post #7 would work just fine.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  4. #19
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by Philip M View Post
    Yes, but that is somewhat specialised. My customers have never asked for card games on their sites. But if they did I feel that the code I posted in post #7 would work just fine.
    sure, it was a close to the matter example. with my array methods, i am used to getting new copies of everything, and i sometime even forget about side-effects.

    just to be complete, we all should note that you can use any mutative method to get a new array with just 8 extra chars: myArray.slice().shuffle(). that works for shuffle(), reverse(), pop(), anything. in that light, it makes sense to include only the mutative versions, since it's a pain to make a transformer a mutator (using [].push.apply), but it's easy to clone just in time so as to turn a mutator into a transformer.
    Last edited by rnd me; 08-13-2013 at 04:48 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  5. Users who have thanked rnd me for this post:

    phantom007 (08-17-2013)

  6. #20
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    892
    Thanks
    301
    Thanked 2 Times in 2 Posts
    2 more questions

    1) Whats the difference between the following two codes?
    2) What benefit do I get when I use the prototype property?

    1)
    Code:
    function displayInfo(fname){
      this.fname;
    }
    
    displayInfo.age = 25;
    
    console.log(displayInfo.age);

    2)
    Code:
    function displayInfo(fname){
      this.fname = fname;
    }
    
    sam = new displayInfo('Sam');
    displayInfo.prototype.age = null;
    sam.age = 33;
    console.log(sam.age);

  7. #21
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    not sure what code 1) is supposed to be. despite that, displayInfo is an object of type function. you could also say it has a static property (named age).

    code 2) has at least a useful constructor (i.e. it actually uses the passed parameter). here sam is an Object (resp. displayInfo) object.

    other than that I don’t see what you need those constructors for (in that example). doing
    Code:
    var obj = {};
    obj.age = 42;
    console.log(obj.age);
    has pretty much the same effect (though the displayInfo objects from code 2 do have a default value for the age property)
    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

  8. #22
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    let’s do some inspection
    Code:
    function displayInfo(fname){
      this.fname = fname; // make it comparable to code 2
    }
    
    typeof displayInfo // "function"
    displayInfo.age // undefined
    displayInfo.fname // undefined
    
    displayInfo.age = 25;
    
    displayInfo.age // 25
    
    var sam = new displayInfo("Sam");
    
    typeof sam // "object"
    sam.age // undefined
    sam.fname // "Sam"
    2)
    Code:
    function displayInfo(fname){
      this.fname = fname;
    }
    
    typeof displayInfo // "function"
    displayInfo.age // undefined
    displayInfo.fname // undefined
    
    sam = new displayInfo('Sam');
    
    typeof sam // "object"
    sam.age // undefined
    sam.fname // "Sam"
    
    displayInfo.prototype.age = null;
    
    sam.age // null
    
    sam.age = 33;
    
    sam.age // 33
    
    // and now for something funny
    var joe = new displayInfo("Joe");
    
    joe.age // null
    // reason: you cannot set a property of the prototype. 
    // doing so will create a local property in the created object
    
    Object.keys(sam); // ["fname", "age"]
    Object.keys(joe); // ["fname"]
    Last edited by Dormilich; 08-17-2013 at 12:37 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

  9. Users who have thanked Dormilich for this post:

    phantom007 (08-18-2013)


 
Page 2 of 2 FirstFirst 12

Posting Permissions

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