SlideShare una empresa de Scribd logo
1 de 13
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

Swift で JavaScript 始めませんか? #iOSDC
Swift で JavaScript 始めませんか? #iOSDCSwift で JavaScript 始めませんか? #iOSDC
Swift で JavaScript 始めませんか? #iOSDCTomohiro 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 RxSwiftFlorent 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 RxSwiftScott 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é rousselHervé Vũ Roussel
 
Presenting things in Swift
Presenting things in SwiftPresenting things in Swift
Presenting things in SwiftDenis Fileev
 
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 analysisLiang (Leon) Zhou
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascriptAbimbola Idowu
 
Javascript this keyword
Javascript this keywordJavascript this keyword
Javascript this keywordPham Huy Tung
 
Functional Reactive Programming - RxSwift
Functional Reactive Programming - RxSwiftFunctional Reactive Programming - RxSwift
Functional Reactive Programming - RxSwiftRodrigo Leite
 
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 Introzhang tao
 

La actualidad más candente (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 a this is simple

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 JavaScriptDonald 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 #burningkeyboardsDenis Ristic
 
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 JavaScriptNascenia IT
 
Ian 20150116 java script oop
Ian 20150116 java script oopIan 20150116 java script oop
Ian 20150116 java script oopLearningTech
 
Javascript basics
Javascript basicsJavascript basics
Javascript basicsFin Chen
 
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 AppsKris Wallsmith
 
Javascript Secrets - Front in Floripa 2015
Javascript Secrets - Front in Floripa 2015Javascript Secrets - Front in Floripa 2015
Javascript Secrets - Front in Floripa 2015Fernando Daciuk
 

Similar a 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
 

Más de Nir Elbaz

Responsive Web Design
Responsive Web DesignResponsive Web Design
Responsive Web DesignNir Elbaz
 
Hello, AngularJS 1.3
Hello, AngularJS 1.3Hello, AngularJS 1.3
Hello, AngularJS 1.3Nir 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 JavaScriptNir Elbaz
 
IBM Worklight
IBM WorklightIBM Worklight
IBM WorklightNir Elbaz
 
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajaxNir Elbaz
 
Introduction to html 5
Introduction to html 5Introduction to html 5
Introduction to html 5Nir Elbaz
 

Más de 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
 

Último

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 

Último (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 

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