javascript this keyword

22
Javascript ‘this’ keyword Pham Tung - Aug 2015

Upload: pham-huy-tung

Post on 19-Feb-2017

1.082 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Javascript this keyword

Javascript‘this’ keyword

Pham Tung - Aug 2015

Page 2: Javascript this keyword

What is this?

Page 3: Javascript this keyword

Definitionthis is a special identifier keyword - pointing to the “owner” of the function being executed.

The value of this is based on the context in which the function is called at runtime.

Page 4: Javascript this keyword

Global Context

console.log(this === window); // true

Page 5: Javascript this keyword

Function Contextfunction f1(){ return this;}

f1() === window; // global object

Page 6: Javascript this keyword

Constructorfunction Foo(){

this.value = 1;console.log(this);

}

var obj = new Foo();console.log(obj.value); // logs 1

Same object

Page 7: Javascript this keyword

‘this’ in Nested Functionvar obj = {};obj.func1 = function() { console.log(1, this); function func2() { console.log(2, this); } func2();} obj.func1();

Object {}

Window

Page 8: Javascript this keyword

In–line event handler<element onclick="doStuff()">

window

HTML ElementdoStuff()

this

Page 9: Javascript this keyword

DOM event handlerelement.onclick = doStuff;

window

doStuff()HTML Element this

Page 10: Javascript this keyword

Strict Mode"use strict";function test(){ alert(this); }

test();window.test();

undefined

[object Window]

(function(){alert(this);

})();

Page 11: Javascript this keyword

eval() vs setTimeout()/setInterval()var obj = {};obj.testEval = function () { eval("alert(this)");};obj.testSetTimeout = function () { setTimeout("alert(this)", 100);};

obj.testEval(); // objobj.testSetTimeout(); // window

the setTimeout function is a method of the window object.

Any code inside the anonymous function that is passed into the setTimeout call has window as it’s context

Page 12: Javascript this keyword

Execution Context Syntax of function call Value of this

Global n/a global object (e.g. window)

Function Method call:myObject.foo();

myObject

Function Baseless function call:foo();

global object (e.g. window)(undefined in strict mode)

Function Using call:foo.call(context, myArg);

context

Function Using apply:foo.apply(context, [myArgs]);

context

Function Constructor with new:var new Foo = new Foo();

the new instance(e.g. newFoo)

Evaluation n/a value of this in parent contexteval()

Page 13: Javascript this keyword

Controlling the Value of this

Page 14: Javascript this keyword

Changing ‘this’ contextvar obj = {value: 100};function add(num1, num2){

alert(this.value + num1 + num2);}add.call(obj, 200, 300);add.apply(obj, [200, 300]);var objAdd= add.bind(obj); objAdd(200, 300);

Page 15: Javascript this keyword

that = thisvar boy = {

name: "Bill",girlFriends: ["Emma", "Mia", "Ava"],showGirlFriends: function () {

var that = this;this.girlFriends.forEach (function (item) {

console.log (that.name + " has a girlfriend called " + item);

});}

};boy.showGirlFriends();

Page 16: Javascript this keyword

Quiz

Page 17: Javascript this keyword

Quiz 1function alertThis(){ alert(this); }

(function(){ eval('alertThis()');})();

Page 18: Javascript this keyword

Quiz 2var obj = {};obj.test = function () { setTimeout(function () { alert(this); }, 100);};obj.test();

Page 19: Javascript this keyword

Quiz 3<input type="button" id="button1" value="Button 1" onclick="test(this)" /><input type="button" id="button2" value="Button 2" />

window.test = function(){ alert(this);}document.getElementById("button2").onclick = test;

http://jsfiddle.net/yinyang/fju34vkg/

Page 20: Javascript this keyword

Quiz 4<input type="button" id="button" value="Click Me" />function Foo() { this.value = 100; this.showValue = function () { alert(this.value); } } var foo = new Foo();foo.showValue(); // (1)document.getElementById("button").onclick = foo.showValue; // (2)

http://jsfiddle.net/yinyang/tLuseowc/

Page 22: Javascript this keyword

The End!