<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <h2>JavaScript <b>this</b> 关键字</h2> <p>实例中,<b>this</b> 指向了 <b>person</b> 对象。</p> <p>因为 person 对象是 fullName 方法的所有者。</p> <p id="demo"></p> <script> // 创建一个对象 var person = { firstName: "John", lastName : "Doe", id : 5566, fullName : function() { return this.firstName + " " + this.lastName; } }; // 显示对象的数据 document.getElementById("demo").innerHTML = person.fullName(); </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <h2>JavaScript <b>this</b> 关键字</h2> <p>实例中,<b>this</b> 指向了 <b>person</b> 对象。</p> <p>因为 person 对象是 fullName 方法的所有者。</p> <p id="demo"></p> <script> // 创建一个对象 var person = { firstName: "John", lastName : "Doe", id : 5566, fullName : function() { return this.firstName + " " + this.lastName; } }; // 显示对象的数据 document.getElementById("demo").innerHTML = person.fullName(); </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <h2>JavaScript <b>this</b> 关键字</h2> <p>实例中,<b>this</b> 指向了 window 对象:</p> <p id="demo"></p> <script> var x = this; document.getElementById("demo").innerHTML = x; </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <h2>JavaScript <b>this</b> 关键字</h2> <p>实例中,<b>this</b> 指向了 window 对象:</p> <p id="demo"></p> <script> "use strict"; var x = this; document.getElementById("demo").innerHTML = x; </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <h2>JavaScript <b>this</b> 关键字</h2> <p>实例中,<b>this</b> 表示 myFunction 函数的所有者:</p> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = myFunction(); function myFunction() { return this; } </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <h2>JavaScript <b>this</b> 关键字</h2> <button onclick="this.style.display='none'">点我后我就消失了</button> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <h2>JavaScript <b>this</b> 关键字</h2> <p>在实例中,<b>this</b> 指向了 fullName 方法所属的对象 person。</p> <p id="demo"></p> <script> // 创建一个对象 var person = { firstName : "John", lastName : "Doe", id : 5566, myFunction : function() { return this; } }; // 显示表单数据 document.getElementById("demo").innerHTML = person.myFunction(); </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <h2>JavaScript <b>this</b> 关键字</h2> <p>实例中,<b>this</b> 指向了 <b>person</b> 对象。</p> <p>因为 person 对象是 fullName 方法的所有者。</p> <p id="demo"></p> <script> // 创建一个对象 var person = { firstName: "John", lastName : "Doe", id : 5566, fullName : function() { return this.firstName + " " + this.lastName; } }; // 显示对象的数据 document.getElementById("demo").innerHTML = person.fullName(); </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <h2>JavaScript this 关键字</h2> <p>实例中 <strong>this</strong> 指向了 person2,即便它是 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>