https://www.w3schools.com/js/tryit.asp?filename=tryjs_this_method <!DOCTYPE html> <html> <body> <h2>The JavaScript <b>this</b> Keyword</h2> <p>In this example, <b>this</b> represents the <b>person</b> object.</p> <p>Because the person object "owns" the fullName method.</p> <p id="demo"></p> <script> // Create an object: var person = { firstName: "John", lastName : "Doe", id : 5566, fullName : function() { return this.firstName + " " + this.lastName; } }; // Display data from the object: document.getElementById("demo").innerHTML = person.fullName(); </script> </body> </html> ? La this parola chiave JavaScript fa riferimento all?oggetto a cui appartiene. Ha valori diversi a seconda di dove viene utilizzato: ? In un metodo, si this riferisce all?oggetto proprietario . ? Da solo, si this riferisce all?oggetto globale . ? In una funzione, this fa riferimento all?oggetto globale . ? In una funzione, in modalit? rigorosa, lo this ? undefined. ? In un evento, si this riferisce all?elemento che ha ricevuto l?evento. ? Metodi come call(), e apply() pu? riferirsi thisa qualsiasi oggetto . ? In un metodo oggetto, thisf a riferimento al " proprietario " del metodo. ? Nell?esempio in cima a questa pagina, this fa riferimento all?oggetto persona . ? L? oggetto persona ? il proprietario del metodo fullName . https://www.w3schools.com/js/tryit.asp?filename=tryjs_this <!DOCTYPE html> <html> <body> <h2>The JavaScript <b>this</b> Keyword</h2> <p>In this example, <b>this</b> refers to the Object window :</p> <p id="demo"></p> <script> var x = this; document.getElementById("demo").innerHTML = x; </script> </body> </html> https://www.w3schools.com/js/tryit.asp?filename=tryjs_this_function <!DOCTYPE html> <html> <body> <h2>The JavaScript <b>this</b> Keyword</h2> <p>In this example, <b>this</b> represents the object that "owns" myFunction:</p> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = myFunction(); function myFunction() { return this; } </script> </body> </html> ? THIS in una funzione (predefinito) ? In una funzione JavaScript, il proprietario della funzione ? l? associazione predefinita per this. ? Quindi, in una funzione, si thisriferisce all?oggetto Globale [object Window]. ? La modalit? rigorosa JavaScript non consente il binding predefinito. ? Quindi, se usato in una funzione, in modalit? rigorosa, lo this? undefined. https://www.w3schools.com/js/tryit.asp?filename=tryjs_this_function <!DOCTYPE html> <html> <body> <h2>The JavaScript <b>this</b> Keyword</h2> <p>In a function, by default, <b>this</b> refers to the Global object.</p> <p>In strict mode, <b>this</b> is <b>undefined</b>, because strict mode does not allow default binding:</p> <p id="demo"></p> <script> "use strict"; document.getElementById("demo").innerHTML = myFunction(); function myFunction() { return this; } </script> </body> </html> Nei gestori di eventi HTML, thisfa riferimento all?elemento HTML che ha ricevuto l?evento: https://www.w3schools.com/js/tryit.asp?filename=tryjs_this_event <!DOCTYPE html> <html> <body> <h2>The JavaScript <b>this</b> Keyword</h2> <button onclick="this.style.display=?non?">Click to Remove Me!</button> </body> </html> ? Binding del metodo Object ? In questi esempi, this ? l? oggetto persona (L?oggetto persona ? il "proprietario" della funzione): https://www.w3schools.com/js/tryit.asp?filename=tryjs_this_object <!DOCTYPE html> <html> <body> <h2>The JavaScript <b>this</b> Keyword</h2> <p>In this example, <b>this</b> represents the person object that "owns" the fullName method.</p> <p id="demo"></p> <script> // Create an object: var person = { firstName : "John", lastName : "Doe", id : 5566, myFunction : function() { return this; } }; // Display data from the object: document.getElementById("demo").innerHTML = person.myFunction(); </script> </body> </html> https://www.w3schools.com/js/tryit.asp?filename=tryjs_this_method ? In altre parole: this.firstName indica la propriet? firstName di questo oggetto (persona). <!DOCTYPE html> <html> <body> <h2>The JavaScript <b>this</b> Keyword</h2> <p>In this example, <b>this</b> represents the <b>person</b> object.</p> <p>Because the person object "owns" the fullName method.</p> <p id="demo"></p> <script> // Create an object: var person = { firstName: "John", lastName : "Doe", id : 5566, fullName : function() { return this.firstName + " " + this.lastName; } }; // Display data from the object: document.getElementById("demo").innerHTML = person.fullName(); </script> </body> </html> ? Binding di funzioni esplicite ? I metodi call() e apply() sono metodi JavaScript predefiniti. ? Possono essere entrambi utilizzati per chiamare un metodo oggetto con un altro oggetto come argomento. ? Puoi leggere di pi? su call() e apply() pi? avanti in questo tutorial. ? Nell?esempio seguente, quando si chiama person1.fullName con person2 come argomento, si this far? riferimento a person2, anche se si tratta di un metodo di person1: https://www.w3schools.com/js/tryit.asp?filename=tryjs_this_call <!DOCTYPE html> <html> <body> <h2>The JavaScript this Keyword</h2> <p>In this example <strong>this</strong> refers to person2, even if it is a method of person1:</p> <p id="demo"></p> <script> var person1 = { fullName: function() { return this.firstName + " " + this.lastName; } } var person2 = { firstName:"John", lastName: "Doe", } var x = person1.fullName.call(person2); document.getElementById("demo").innerHTML = x; </script> </body> </html> |