Jump to content

Jim12345

Members
  • Posts

    5
  • Joined

  • Last visited

Posts posted by Jim12345

  1. Dave,

     

    That is good to know but I was thinking it doesn't help me on this issue.

     

    Old school code would look like this:

    var saver = function (value) {return value;    };alert (saver(10));

    I am still getting my 10 but without the extra function in a function.

     

    Maybe a better example of a closure might help me here?

     

    One that it is clear that, "now I know why closures are important" ?

     

    Jim

  2. Hello,

     

    I am looking over code plus the concept of closures. Below is the code:

     

    Closures are a powerful tool but can be tricky to understand. Mastering them, however, will allow you to write some very elegant code.

    Here’s an example:

    var saver = function (value) {    return function () {        return value;    };};var retriever = saver(10);alert(retriever());

    So the code alerts 10. If you rewrite the last line to be console.log(retriever);

     

    You get this:

     

    function () { return value; }

     

    I can watch the original code execute in Chrome Debugging Tools, but it doesn't help to understand it.

     

    Does anyone know I resource where I can watch say a table where the functions get created and variables get passed? Or can you create the table right here?

     

    Years ago a read a book about C and the author used tables to simulate memory locations for say functions, pointer to functions, variables and

    passing values by reference, etc.

     

    Thanks,

    Jim

     

     

  3. Hello,

     

    I have a question about prototypes. It seems to me I always encounter a new method being added to an object after the fact. For example this code below runs fine.

    <script>var GameObject = function(width, height) { this.x = Math.floor((Math.random() ) + 1); this.y = Math.floor((Math.random() ) + 1); this.width = width; this.height = height; this.car='AMC'; return this;};GameObject.prototype = { draw: function() { return (this.width); }};var n = new GameObject(2200,400);console.log(n.draw());</script>

     

    But what I want to do is right from the get go, have the method draw as part of the protoype, this I can't figure out. So what would the code look like?

     

    Thanks,

    Jim P.

×
×
  • Create New...