February 2019
Beginner to intermediate
284 pages
6h 20m
English
Functions are typically created in JavaScript using the function keyword, using one of the following forms:
function f() { console.log('function1', this);}const g = function(name) { console.log('function ' + name, this);}f(); // calls fg('test'); // calls g() with a parameter
The this keyword refers to the object that owns the function. If this code runs in a browser, and this is a top-level function created in the <script> block, the owner is the global window object. Any properties accessed via this refer to that object.
A function can be placed in the scope of an object, behaving as a method. The this reference in the following code refers to the obj object and can access this.a and this.b:
const obj = {a: 5, b: 6}obj.method ...Read now
Unlock full access