SlideShare a Scribd company logo
this
is simple

Nir Elbaz, 2013

1
this is a JavaScript language immutable variable

Nir Elbaz, 2013

2
Usually, this refers to the object which owns
the method it was called from

Nir Elbaz, 2013

3
this === window (global context)

// no current object - window object owns it all:
var strText = "Hello, world";
alert(strText);
alert(window.strText);
alert(this.strText);

// -> "Hello, world"
// -> again, "Hello, world"
// -> and again, "Hello, world"

Nir Elbaz, 2013

4
this === window (global context)

// callingThis method is owned by window object:
var strText = "Hello, world";
function callingThis () {
alert(strText);
alert(window.strText);
alert(this.strText);
}

// -> "Hello, world"
// -> again, "Hello, world"
// -> and again, "Hello, world"

callingThis();

Nir Elbaz, 2013

5
this === undefined

// adding “use strict” to a function will cause this to be undefined:
var strText = "Hello, world";
function callingThis () {
"use strict";
alert(strText);
alert(window.strText);
alert(this.strText);
}

// -> "Hello, world"
// -> again, "Hello, world"
// -> nothing is shown since this === undefined

callingThis();

Nir Elbaz, 2013

6
this === Object
// callingThis, callingThat & callingThatAgain methods are now owned by
// MainObject object:
this.strText = "Hello, world";
function MainObject () {
this.strText = "I belong to MainObject";
this.callingThis = function () {
alert(this.strText);
}
}
MainObject.prototype.callingThat = function () {
alert(this.strText);
}
function alertText () {
alert(this.strText);
}
var obj = new MainObject();
obj.callingThis();
// -> "I belong to mainObject"
obj.callingThat();
// -> again, "I belong to mainObject"
obj.callingThatAgain = alertText;
obj.callingThatAgain();
// -> and Nir Elbaz, 2013 belong to mainObject"
again, "I
alert(this.strText);
// -> "Hello, world"
alertText();
// -> again, "Hello, world"

7
this === Object
// using bind, apply & call to define this:
var strText = "Hello, world";
function callingThis () {
alert(this.strText);
}
function MyObject () {
this.strText = "I belong to MainObject";
}
CallingThis();
var myObject = new MyObject();
callingThis.apply(myObject);
callingThis.call(myObject);
callingThis.bind(myObject);

// -> "Hello, world"
// -> "I belong to mainObject"
// -> again, "I belong to mainObject"
// -> and again, "I belong to mainObject"

Nir Elbaz, 2013

8
this === Object... and window!
// in ECMA 3, -this- refers to the head object in nested functions:
var MyObject = {
func1: function () {
console.log(this); // logs MyObject
var func2 = function () {
console.log(this); // logs window, and will do so from this point on
var func3 = function () {
console.log(this); // logs window, as it’s the head object
}();
}();
}
};
MyObject.func1();

Nir Elbaz, 2013

9
this === Object
// ECMA 5 fixed this behavior, or - you can fix it by sending -this- as a param
var MyObject = {
func1: function () {
console.log(this);
// logs MyObject
var func2 = function (self) {
console.log(self);
// logs MyObject
var func3 = function (self) {
console.log(self); // logs MyObject
}(self);
}(this);
}
};
MyObject.func1();

Nir Elbaz, 2013

10
this === DOM Element
// when a function is used as an event handler, its -this- is set to the element
// the event fired from, also in an inline event handler
var paragraphs = document.getElementsByTagName('p');
for (var i = 0, j = paragraphs.length; i < j; i++) {
paragraphs[i].addEventListener('click', function () {
console.log(this); // logs the element the event fired from
}, false)
}
<button onclick="console.log(this);">Log this</button>

Nir Elbaz, 2013

11
Summary
When not using call / apply or bind, the this value
will always refer to the global context, except in the
following instances:
✔

✔

The function was called with the new operator, in which
case this points to a new object
The function is a member of an object, in which case
this points to the object UNLESS function is being
called asynchronously.
- Yehuda Katz
Nir Elbaz, 2013

12
Resources
●

Fully Understanding the this Keyword

●

What is ‘this’ in JavaScript?

●

this

●

Understanding “Prototypes” in JavaScript

http://nirelbaz.tumblr.com

Nir Elbaz, 2013

13

More Related Content

What's hot

AngularJs , How it works
AngularJs , How it worksAngularJs , How it works
AngularJs , How it works
Jayantha Sirisena
 
JavaScript Looping Statements
JavaScript Looping StatementsJavaScript Looping Statements
JavaScript Looping Statements
Janssen Harvey Insigne
 
Swift で JavaScript 始めませんか? #iOSDC
Swift で JavaScript 始めませんか? #iOSDCSwift で JavaScript 始めませんか? #iOSDC
Swift で JavaScript 始めませんか? #iOSDC
Tomohiro Kumagai
 
AnyObject – 自分が見落としていた、基本の話
AnyObject – 自分が見落としていた、基本の話AnyObject – 自分が見落としていた、基本の話
AnyObject – 自分が見落としていた、基本の話
Tomohiro Kumagai
 
Reactive Programming Patterns with RxSwift
Reactive Programming Patterns with RxSwiftReactive Programming Patterns with RxSwift
Reactive Programming Patterns with RxSwift
Florent Pillet
 
Javascript & Ajax Basics
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax BasicsRichard Paul
 
Reactive Programming with RxSwift
Reactive Programming with RxSwiftReactive Programming with RxSwift
Reactive Programming with RxSwift
Scott Gardner
 
Groovify your java code by hervé roussel
Groovify your java code by hervé rousselGroovify your java code by hervé roussel
Groovify your java code by hervé roussel
Hervé Vũ Roussel
 
Presenting things in Swift
Presenting things in SwiftPresenting things in Swift
Presenting things in Swift
Denis Fileev
 
Boot strap.groovy
Boot strap.groovyBoot strap.groovy
Boot strap.groovy
Vijay Shukla
 
Example sas code for ICC calculation and timeseries analysis
Example sas code for ICC calculation and timeseries analysisExample sas code for ICC calculation and timeseries analysis
Example sas code for ICC calculation and timeseries analysis
Liang (Leon) Zhou
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
Abimbola Idowu
 
Javascript this keyword
Javascript this keywordJavascript this keyword
Javascript this keyword
Pham Huy Tung
 
Json.parse() in JavaScript
Json.parse() in JavaScriptJson.parse() in JavaScript
Json.parse() in JavaScript
Ideas2IT Technologies
 
Functional Reactive Programming - RxSwift
Functional Reactive Programming - RxSwiftFunctional Reactive Programming - RxSwift
Functional Reactive Programming - RxSwift
Rodrigo Leite
 
Function
FunctionFunction
Function
venkatme83
 
Constructor in detail
Constructor in detailConstructor in detail
Constructor in detail
HSS-Software House
 
Php 5.6
Php 5.6Php 5.6
Orlando BarCamp Why Javascript Doesn't Suck
Orlando BarCamp Why Javascript Doesn't SuckOrlando BarCamp Why Javascript Doesn't Suck
Orlando BarCamp Why Javascript Doesn't Suckerockendude
 
Ruby on Rails Intro
Ruby on Rails IntroRuby on Rails Intro
Ruby on Rails Intro
zhang tao
 

What's hot (20)

AngularJs , How it works
AngularJs , How it worksAngularJs , How it works
AngularJs , How it works
 
JavaScript Looping Statements
JavaScript Looping StatementsJavaScript Looping Statements
JavaScript Looping Statements
 
Swift で JavaScript 始めませんか? #iOSDC
Swift で JavaScript 始めませんか? #iOSDCSwift で JavaScript 始めませんか? #iOSDC
Swift で JavaScript 始めませんか? #iOSDC
 
AnyObject – 自分が見落としていた、基本の話
AnyObject – 自分が見落としていた、基本の話AnyObject – 自分が見落としていた、基本の話
AnyObject – 自分が見落としていた、基本の話
 
Reactive Programming Patterns with RxSwift
Reactive Programming Patterns with RxSwiftReactive Programming Patterns with RxSwift
Reactive Programming Patterns with RxSwift
 
Javascript & Ajax Basics
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax Basics
 
Reactive Programming with RxSwift
Reactive Programming with RxSwiftReactive Programming with RxSwift
Reactive Programming with RxSwift
 
Groovify your java code by hervé roussel
Groovify your java code by hervé rousselGroovify your java code by hervé roussel
Groovify your java code by hervé roussel
 
Presenting things in Swift
Presenting things in SwiftPresenting things in Swift
Presenting things in Swift
 
Boot strap.groovy
Boot strap.groovyBoot strap.groovy
Boot strap.groovy
 
Example sas code for ICC calculation and timeseries analysis
Example sas code for ICC calculation and timeseries analysisExample sas code for ICC calculation and timeseries analysis
Example sas code for ICC calculation and timeseries analysis
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 
Javascript this keyword
Javascript this keywordJavascript this keyword
Javascript this keyword
 
Json.parse() in JavaScript
Json.parse() in JavaScriptJson.parse() in JavaScript
Json.parse() in JavaScript
 
Functional Reactive Programming - RxSwift
Functional Reactive Programming - RxSwiftFunctional Reactive Programming - RxSwift
Functional Reactive Programming - RxSwift
 
Function
FunctionFunction
Function
 
Constructor in detail
Constructor in detailConstructor in detail
Constructor in detail
 
Php 5.6
Php 5.6Php 5.6
Php 5.6
 
Orlando BarCamp Why Javascript Doesn't Suck
Orlando BarCamp Why Javascript Doesn't SuckOrlando BarCamp Why Javascript Doesn't Suck
Orlando BarCamp Why Javascript Doesn't Suck
 
Ruby on Rails Intro
Ruby on Rails IntroRuby on Rails Intro
Ruby on Rails Intro
 

Similar to this is simple

Javascript tid-bits
Javascript tid-bitsJavascript tid-bits
Javascript tid-bits
David Atchley
 
Javascript quiz. Questions to ask when recruiting developers.
Javascript quiz. Questions to ask when recruiting developers.Javascript quiz. Questions to ask when recruiting developers.
Javascript quiz. Questions to ask when recruiting developers.
Alberto Naranjo
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
Donald Sipe
 
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
Doug Jones
 
05 JavaScript #burningkeyboards
05 JavaScript #burningkeyboards05 JavaScript #burningkeyboards
05 JavaScript #burningkeyboards
Denis Ristic
 
6976.ppt
6976.ppt6976.ppt
JavaScript Closures for Dummies & JavaScript prototype, closures and OOP.
JavaScript Closures for Dummies & JavaScript prototype, closures and OOP.JavaScript Closures for Dummies & JavaScript prototype, closures and OOP.
JavaScript Closures for Dummies & JavaScript prototype, closures and OOP.
Jin-Hwa Kim
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
Nascenia IT
 
"this" in JavaScript
"this" in JavaScript"this" in JavaScript
"this" in JavaScript
Martha Schumann
 
Ian 20150116 java script oop
Ian 20150116 java script oopIan 20150116 java script oop
Ian 20150116 java script oop
LearningTech
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
Fin Chen
 
DrupalCon jQuery
DrupalCon jQueryDrupalCon jQuery
DrupalCon jQuery
Nathan Smith
 
Java script for web developer
Java script for web developerJava script for web developer
Java script for web developer
Chalermpon Areepong
 
Java script
Java scriptJava script
Java script
Adrian Caetano
 
Fewd week5 slides
Fewd week5 slidesFewd week5 slides
Fewd week5 slides
William Myers
 
Say It With Javascript
Say It With JavascriptSay It With Javascript
Say It With Javascript
Giovanni Scerra ☃
 
The mighty js_function
The mighty js_functionThe mighty js_function
The mighty js_functiontimotheeg
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony Apps
Kris Wallsmith
 
Javascript Secrets - Front in Floripa 2015
Javascript Secrets - Front in Floripa 2015Javascript Secrets - Front in Floripa 2015
Javascript Secrets - Front in Floripa 2015
Fernando Daciuk
 

Similar to this is simple (20)

Javascript tid-bits
Javascript tid-bitsJavascript tid-bits
Javascript tid-bits
 
Javascript quiz. Questions to ask when recruiting developers.
Javascript quiz. Questions to ask when recruiting developers.Javascript quiz. Questions to ask when recruiting developers.
Javascript quiz. Questions to ask when recruiting developers.
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
 
05 JavaScript #burningkeyboards
05 JavaScript #burningkeyboards05 JavaScript #burningkeyboards
05 JavaScript #burningkeyboards
 
Javascript
JavascriptJavascript
Javascript
 
6976.ppt
6976.ppt6976.ppt
6976.ppt
 
JavaScript Closures for Dummies & JavaScript prototype, closures and OOP.
JavaScript Closures for Dummies & JavaScript prototype, closures and OOP.JavaScript Closures for Dummies & JavaScript prototype, closures and OOP.
JavaScript Closures for Dummies & JavaScript prototype, closures and OOP.
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
"this" in JavaScript
"this" in JavaScript"this" in JavaScript
"this" in JavaScript
 
Ian 20150116 java script oop
Ian 20150116 java script oopIan 20150116 java script oop
Ian 20150116 java script oop
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
 
DrupalCon jQuery
DrupalCon jQueryDrupalCon jQuery
DrupalCon jQuery
 
Java script for web developer
Java script for web developerJava script for web developer
Java script for web developer
 
Java script
Java scriptJava script
Java script
 
Fewd week5 slides
Fewd week5 slidesFewd week5 slides
Fewd week5 slides
 
Say It With Javascript
Say It With JavascriptSay It With Javascript
Say It With Javascript
 
The mighty js_function
The mighty js_functionThe mighty js_function
The mighty js_function
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony Apps
 
Javascript Secrets - Front in Floripa 2015
Javascript Secrets - Front in Floripa 2015Javascript Secrets - Front in Floripa 2015
Javascript Secrets - Front in Floripa 2015
 

More from Nir Elbaz

Responsive Web Design
Responsive Web DesignResponsive Web Design
Responsive Web Design
Nir Elbaz
 
Hello, AngularJS 1.3
Hello, AngularJS 1.3Hello, AngularJS 1.3
Hello, AngularJS 1.3
Nir Elbaz
 
Dalek.js - Automated cross browser testing with JavaScript
Dalek.js - Automated cross browser testing with JavaScriptDalek.js - Automated cross browser testing with JavaScript
Dalek.js - Automated cross browser testing with JavaScript
Nir Elbaz
 
IBM Worklight
IBM WorklightIBM Worklight
IBM Worklight
Nir Elbaz
 
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajax
Nir Elbaz
 
Introduction to html 5
Introduction to html 5Introduction to html 5
Introduction to html 5
Nir Elbaz
 

More from Nir Elbaz (6)

Responsive Web Design
Responsive Web DesignResponsive Web Design
Responsive Web Design
 
Hello, AngularJS 1.3
Hello, AngularJS 1.3Hello, AngularJS 1.3
Hello, AngularJS 1.3
 
Dalek.js - Automated cross browser testing with JavaScript
Dalek.js - Automated cross browser testing with JavaScriptDalek.js - Automated cross browser testing with JavaScript
Dalek.js - Automated cross browser testing with JavaScript
 
IBM Worklight
IBM WorklightIBM Worklight
IBM Worklight
 
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajax
 
Introduction to html 5
Introduction to html 5Introduction to html 5
Introduction to html 5
 

Recently uploaded

GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
ThomasParaiso2
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 

Recently uploaded (20)

GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 

this is simple

  • 2. this is a JavaScript language immutable variable Nir Elbaz, 2013 2
  • 3. Usually, this refers to the object which owns the method it was called from Nir Elbaz, 2013 3
  • 4. this === window (global context) // no current object - window object owns it all: var strText = "Hello, world"; alert(strText); alert(window.strText); alert(this.strText); // -> "Hello, world" // -> again, "Hello, world" // -> and again, "Hello, world" Nir Elbaz, 2013 4
  • 5. this === window (global context) // callingThis method is owned by window object: var strText = "Hello, world"; function callingThis () { alert(strText); alert(window.strText); alert(this.strText); } // -> "Hello, world" // -> again, "Hello, world" // -> and again, "Hello, world" callingThis(); Nir Elbaz, 2013 5
  • 6. this === undefined // adding “use strict” to a function will cause this to be undefined: var strText = "Hello, world"; function callingThis () { "use strict"; alert(strText); alert(window.strText); alert(this.strText); } // -> "Hello, world" // -> again, "Hello, world" // -> nothing is shown since this === undefined callingThis(); Nir Elbaz, 2013 6
  • 7. this === Object // callingThis, callingThat & callingThatAgain methods are now owned by // MainObject object: this.strText = "Hello, world"; function MainObject () { this.strText = "I belong to MainObject"; this.callingThis = function () { alert(this.strText); } } MainObject.prototype.callingThat = function () { alert(this.strText); } function alertText () { alert(this.strText); } var obj = new MainObject(); obj.callingThis(); // -> "I belong to mainObject" obj.callingThat(); // -> again, "I belong to mainObject" obj.callingThatAgain = alertText; obj.callingThatAgain(); // -> and Nir Elbaz, 2013 belong to mainObject" again, "I alert(this.strText); // -> "Hello, world" alertText(); // -> again, "Hello, world" 7
  • 8. this === Object // using bind, apply & call to define this: var strText = "Hello, world"; function callingThis () { alert(this.strText); } function MyObject () { this.strText = "I belong to MainObject"; } CallingThis(); var myObject = new MyObject(); callingThis.apply(myObject); callingThis.call(myObject); callingThis.bind(myObject); // -> "Hello, world" // -> "I belong to mainObject" // -> again, "I belong to mainObject" // -> and again, "I belong to mainObject" Nir Elbaz, 2013 8
  • 9. this === Object... and window! // in ECMA 3, -this- refers to the head object in nested functions: var MyObject = { func1: function () { console.log(this); // logs MyObject var func2 = function () { console.log(this); // logs window, and will do so from this point on var func3 = function () { console.log(this); // logs window, as it’s the head object }(); }(); } }; MyObject.func1(); Nir Elbaz, 2013 9
  • 10. this === Object // ECMA 5 fixed this behavior, or - you can fix it by sending -this- as a param var MyObject = { func1: function () { console.log(this); // logs MyObject var func2 = function (self) { console.log(self); // logs MyObject var func3 = function (self) { console.log(self); // logs MyObject }(self); }(this); } }; MyObject.func1(); Nir Elbaz, 2013 10
  • 11. this === DOM Element // when a function is used as an event handler, its -this- is set to the element // the event fired from, also in an inline event handler var paragraphs = document.getElementsByTagName('p'); for (var i = 0, j = paragraphs.length; i < j; i++) { paragraphs[i].addEventListener('click', function () { console.log(this); // logs the element the event fired from }, false) } <button onclick="console.log(this);">Log this</button> Nir Elbaz, 2013 11
  • 12. Summary When not using call / apply or bind, the this value will always refer to the global context, except in the following instances: ✔ ✔ The function was called with the new operator, in which case this points to a new object The function is a member of an object, in which case this points to the object UNLESS function is being called asynchronously. - Yehuda Katz Nir Elbaz, 2013 12
  • 13. Resources ● Fully Understanding the this Keyword ● What is ‘this’ in JavaScript? ● this ● Understanding “Prototypes” in JavaScript http://nirelbaz.tumblr.com Nir Elbaz, 2013 13