Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    Differenza tra metodi privati e closure

    Ciao.
    Codice PHP:
    function sayHello2(name) {
      var 
    text 'Hello ' name// local variable
      
    var sayAlert = function() { alert(text); }
      return 
    sayAlert;

    Sto facendo un po di confusione
    leggo:
    In JavaScript, if you use the function keyword inside another function, you are creating a closure.
    qui


    ma sayAlert non è anche un metodo
    privato di sayHello2 e text una proprietà
    sempre privata di sayHello2 ?



    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  2. #2
    Appurato che c'è differenza:

    Codice PHP:
    function setupSomeGlobals() {
      
    // Local variable that ends up within closure
      
    var num 666;

      
    // Store some references to functions as global variables
      
    function gAlertNumber() { alert(num); }
      function 
    gIncreaseNumber() { num++; }
      function 
    gSetNumber(x) { num x; }
    }
    /* ERRATO 
    setupSomeGlobals();
    gIncreaseNumber();
    gAlertNumber();
    */
    function setupSomeGlobals() {
      
    // Local variable that ends up within closure
      
    var num 666;
    //Globale
    pippo1000;
      
    // Store some references to functions as global variables
      
    gAlertNumber = function() { alert(num); }
      
    gIncreaseNumber = function() { num++; }
      
    gSetNumber = function(x) { num x; }
    }
    setupSomeGlobals();
    gIncreaseNumber();
    gAlertNumber(); 
    Se qc si vuol pronunciare


    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.