www.webdeveloper.com
Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22

Thread: Function property versus this

  1. #16
    Join Date
    Jul 2007
    Posts
    245
    I see.... sorry to be so insisting, but I have the class-instance (or class-object) way of thinking so etched in my mind that it's hard to think of templates or objects inheriting from objects. The Javascript way to work with objects is so flexible that it's really confusing.

    So in the example above, john and lucy are first created with a "template" (the Person constructor). Both of them are assigned a method called sayHello() by prototyping their "template". Then john's sayHello() method is overridden, not affecting the inheritance in lucy. Is this correct?

  2. #17
    Join Date
    Mar 2011
    Posts
    61
    Yes, that's correct

  3. #18
    Join Date
    Jul 2007
    Posts
    245
    Thank you Kever!

    Let me ask you a last question regarding the example you wrote about caching using a closure. How come it works (I've tested it and it does work) if the outer function doen't have the num parameter defined? (note the comments I added to the code)

    PHP Code:
    var isPrime = (function(){ // no argument assigned
     
    var cache = [];
     function 
    isPrime(num){
      if(
    cache[num] != null){
       return 
    cache[num];
      }
      var 
    prime num != 1;
      for(var 
    i=2i<numi++){
       if(
    num == 0){
        
    prime false;
        break;
       }
      }
      
    cache[num] = prime;
      return 
    prime;
     }
     return 
    isPrime// call to isPrime without a parameter? My logic says it should be: return isPrime(num)
    }()); // self-executing function 

  4. #19
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    i believe youre trying to do something like this:


    Code:
    function isPrime(num){ 
    
      if(isPrime[num] != null){ 
       return isPrime[num]; 
      } 
    
    
      for(var i=Math.floor(Math.sqrt(num)); i>1; i-=2){ 
       if( 0 === (num % i) ){ 
          return isPrime[num] = false ;
       } 
      } 
       
        return !! (num-1) && (isPrime[num] = true) ;
     }

  5. #20
    Join Date
    Mar 2011
    Posts
    61
    The outer (closure) function is self-executing and returning the inner the inner function. The global isPrime becomes the inner isPrime function.
    The scope of the outer function is preserved and can still be accessed by the inner function.

    Another way to write it:
    Code:
    (function(){
     var cache = [];
     isPrime = function isPrime(num){ // create a global variable called isPrime pointing to the inner function
      if(cache[num] != null){
       return cache[num];
      }
      var prime = num != 1; // Everything but 1 can be prime
      for(var i=2; i<num; i++){
       if(num % i == 0){
        prime = false;
        break;
       }
      }
      cache[num] = prime;
      return prime;
     }
    }()); // self-execute

  6. #21
    Join Date
    Jul 2007
    Posts
    245
    Yes, that's correct, but I was trying to understand the structure of Kever's example. I suppose that what the isPrime variable is referencing to is the isPrime function (a reference to the function itself is what is returned in the anonymous function), so that if you call isPrime(5) you're referencing it and passing it the parameter. It was kind of twisted because of using the same name for a variable and a function.

    I think it's enough with this thread. Thanks a lot everyone for your help!

  7. #22
    Join Date
    Mar 2011
    Posts
    61
    It was kind of twisted because of using the same name for a variable and a function.
    It might seem confusing at first, but it helps to recognize those function in debuggers.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles