JavaScript function
中的代码将在“某物”调用它时执行。
当函数定义时,函数内部的代码不会被执行。
当函数被调用时,函数内部的代码就会被执行。
通常使用术语“调用函数”而不是“调用 一个函数”。
也常说“调用函数”、“启动函数”或 “执行一个函数”。
在本教程中,我们将使用 invoke,因为 JavaScript 函数可以在不被调用的情况下被调用。
function myFunction(a, b) {
return a * b;
}
myFunction(10, 2); //
Will return 20
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p>The global function (myFunction) returns the product of the arguments (a ,b):</p>
<p id="demo"></p>
<script>
function myFunction(a, b) {
return a * b;
}
document.getElementById("demo").innerHTML = myFunction(10, 2);
</script>
</body>
</html>
上面的函数不属于任何对象。但在 JavaScript 中 始终是默认的全局对象。
在 HTML 中,默认的全局对象是 HTML 页面本身,因此上面的函数“属于” HTML 页面。
在浏览器中,页面对象是浏览器窗口。上面的函数 自动成为窗口函数。
这是调用 JavaScript 函数的常见方法,但不是一个很好的做法。
全局变量、方法或函数很容易在全局对象中产生名称冲突和错误。
myFunction() 和 window.myFunction() 是同一个函数:
function myFunction(a, b) {
return a * b;
}
window.myFunction(10, 2); // Will also return 20
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p>Global functions automatically become window methods. Invoking myFunction() is the same as invoking window.myFunction().</p>
<p id="demo"></p>
<script>
function myFunction(a, b) {
return a * b;
}
document.getElementById("demo").innerHTML = window.myFunction(10, 2);
</script>
</body>
</html>
在 JavaScript 中,this
关键字指的是一个对象。
哪个对象取决于this
如何被调用(使用或调用)。
this
关键字根据其使用方式引用不同的对象:
在对象方法中,this
引用对象。
单独而言,this
指的是全局对象。
在函数中,this
指的是全局对象。
在函数中,在严格模式下,this
是未定义
。
在事件中,this
指的是接收事件的元素。
call()
、apply()
和 bind 等方法()
可以将 this
引用到任何对象。
this
不是变量。这是一个关键字。您无法更改this
的值。
JavaScript this 教程
当在没有所有者对象的情况下调用函数时,this
的值 成为全局对象。
在网络浏览器中,全局对象是浏览器窗口。
此示例返回窗口对象作为 this
的值:
let x = myFunction();
// x will be the window object
function myFunction() {
return this;
}
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p>In HTML the value of <b>this</b>, in a global function, is the window object.</p>
<p id="demo"></p>
<script>
let x = myFunction();
function myFunction() {
return this;
}
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
将函数作为全局函数调用,会导致 this 的值成为全局对象。
使用窗口对象作为变量很容易使程序崩溃。
在 JavaScript 中,您可以将函数定义为对象方法。
以下示例创建一个对象 (myObject),其中有两个 属性(firstName 和 lastName),以及 方法(全名):
const myObject = {
firstName:"John",
lastName: "Doe",
fullName: function () {
return this.firstName + " " + this.lastName;
}
}
myObject.fullName(); // Will return "John Doe"
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p>myObject.fullName() will return John Doe:</p>
<p id="demo"></p>
<script>
const myObject = {
firstName:"John",
lastName: "Doe",
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
document.getElementById("demo").innerHTML = myObject.fullName();
</script>
</body>
</html>
fullName 方法是一个函数。该函数属于 物体。 myObject 是函数的所有者。
名为 this
的对象是 “拥有”JavaScript 代码。在本例中,this
的值 是myObject。
测试一下!更改 fullName 方法以返回 this
的值:
const myObject = {
firstName:"John",
lastName: "Doe",
fullName: function () {
return this;
}
}
// This will return [object Object] (the owner object)
myObject.fullName();
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p>The value of <b>this</b>, in an object method, is the owner object.</p>
<p id="demo"></p>
<script>
const myObject = {
firstName:"John",
lastName: "Doe",
fullName: function() {
return this;
}
}
document.getElementById("demo").innerHTML = myObject.fullName();
</script>
</body>
</html>
将函数作为对象方法调用,会导致 this
的值 成为物体本身。
如果函数调用前面带有 new
关键字, 它是构造函数调用。
看起来你创建了一个新函数,但由于 JavaScript 函数是 对象实际上创建了一个新对象:
// This is a function constructor:
function myFunction(arg1, arg2) {
this.firstName = arg1;
this.lastName = arg2;
}
// This creates a new object
const myObj = new myFunction("John", "Doe");
// This will return "John"
myObj.firstName;
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p>In this example, myFunction is a function constructor:</p>
<p id="demo"></p>
<script>
function myFunction(arg1, arg2) {
this.firstName = arg1;
this.lastName = arg2;
}
const myObj = new myFunction("John","Doe")
document.getElementById("demo").innerHTML = myObj.firstName;
</script>
</body>
</html>
构造函数调用创建一个新对象。新对象继承了 来自其构造函数的属性和方法。
构造函数中的 this
关键字没有值。
this
的值将是调用函数时创建的新对象。