Visualizzazione dei risultati da 1 a 2 su 2

Discussione: uso di APPLY e CALL

  1. #1

    uso di APPLY e CALL

    ho letto questo utilissimo articolo sull'uso di Apply e Call (incuriosito durante la lettura del codice di bytefx).

    qui si capisce il succo:
    Codice PHP:
    var 10;
    var 
    = { x15 };
    function 
    f(message)
    {
        
    alert(message);
        
    alert(this.x);
    }

    f("invoking f");
    f.call(o"invoking f via call"); 
    prima si ha 10 e poi 15


    la mia domanda da ignorante è questa: ma perchè non funziona direttamente così?
    Codice PHP:
    o.f("invoking f"); 
    :master:
    Farmacia di Jarno - le mie pillole: Cookie [#780810], Dom4Php4 [#1123236], Fade [#1139489], getCssProperty [#1152911]
    Inchinatevi difronte al Prof! Nacchio!

    A me pare che l'uomo vada avanti con la retromarcia

  2. #2
    codice:
    function f(message) {
    	alert(message);
    	alert(this.x);
    };
    var	x = 10,
    	o = {x:15, f:f};
    
    o.f("invoking f");
    ... non ho ancora letto il link ma dato la domanda deduco spieghi ben poco riguardo appy e call


    [edit]
    ho dato una letta veloce ... beh, il tipo è un pò troppo auto-referenziato e di certo non espone i concetti in modo corretto ... esempi

    Every function in JavaScript has a number of attached methods, including toString(), call(), and apply()
    attached di cosa? ... sono native prototypes ... se non si capisce cos'è una proto tanto vale lasciar perndere passare a tutto il resto ...


    every function in JavaScript is an object
    ... e il bello è che Object è una function ...


    You might also wonder what the difference is between a function and a method
    ma anche no, un metodo è una function ... PUNTO!



    Functions stand on their own (there is an alert() function, for example), while methods are functions inside an object's dictionary
    se ... come no, sarà un caso allora che tutte le function che dice lui sono metodi del super object window? ....


    we can use the toString() method on a function object to see its source code
    solo se il toString non è stato ridefinito


    Here we have a global function by the name of f(). f() uses the this keyword to reference x, but notice we don't invoke the function through an instance of an object
    ma che caspita dice che il this è sempre definito poichè ogni function internamente ha un this riferito al super object ?
    codice:
    x = 1;
    function pippo(){alert(this.x)};
    pippo(); // WOW .... leggo 1!!!

    So what object does this reference? this will reference the global object.
    ah ecco ... se lo ometteva del tutto gli scrivevo



    The global object is where we defined the variable x
    chiamarlo window o self stava brutto?


    ... k, basta così, ho fatto fin troppo sfottò

    Formaldehyde a new Ajax PHP Zero Config Error Debugger

    WebReflection @WebReflection

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 © 2025 vBulletin Solutions, Inc. All rights reserved.