SlideShare uma empresa Scribd logo
1 de 49
Journey of a C# developer
into Javascript
Massimo Franciosa
Permit me to introduce myself
Permit me to introduce myself
- From Napoli, Italy
- Software Developer since 2008
- Worked for several companies between Italy & UK
- Optima Italia
- Webuild
- Prama (AsianD8, AsianBride)
- Sporting Index
- Reading Room
Might be useful for...
- Backend developers that occasionally work on Javascript code
- People that can already write in Javascript but never studied it
- might also be useful for frontenders that want to understand differences the
other way round (difference from JavaScript to C#)
WON’T be useful for...
- people already savvy in JavaScript and and in C#
- someone that want to learn JavaScript coding best practices. the code here is
just for explainatory purposes!
Part 1: what is “this”?
Ever wondered why?
function Somefunction(){
var that = this;
...
}
Look at this code… c#
public class Example
{
class Foo
{
private string _value = "a";
public string GetValue()
{
return this._value;
}
}
public void ExampleMainFunction()
{
Foo foo = new Foo();
Func<string> funcRef = foo.GetValue;
string result1 = funcRef();
string result2 = foo.GetValue();
}
}
Look at this code… c#
public class Example
{
class Foo
{
private string _value = "a";
public string GetValue()
{
return this._value;
}
}
public void ExampleMainFunction()
{
Foo foo = new Foo();
Func<string> funcRef = foo.GetValue;
string result1 = funcRef();
string result2 = foo.GetValue();
}
}
result1 == “a”
result2 == “a”
Look at this code… JavaScript
var foo = {
value : 'a',
getResult: function() {
return this.value;
}
};
function exampleMainFunction() {
var funcRef = foo.getResult;
var result1 = funcRef();
var result2 = foo.getResult();
console.log(result1);
console.log(result2);
}
Look at this code… JavaScript
var foo = {
value : 'a',
getResult: function() {
return this.value;
}
};
function exampleMainFunction() {
var funcRef = foo.getResult;
var result1 = funcRef();
var result2 = foo.getResult();
console.log(result1);
console.log(result2);
}
result1 == “a”
result2 == “a”
Look at this code… JavaScript
var foo = {
value : 'a',
getResult: function() {
return this.value;
}
};
function exampleMainFunction() {
var funcRef = foo.getResult;
var result1 = funcRef();
var result2 = foo.getResult();
console.log(result1);
console.log(result2);
}
result1 == “a”
result2 == “a”
Look at this code… JavaScript
var foo = {
value : 'a',
getResult: function() {
return this.value;
}
};
function exampleMainFunction() {
var funcRef = foo.getResult;
var result1 = funcRef();
var result2 = foo.getResult();
console.log(result1);
console.log(result2);
}
result1 == undefined
result2 == “a”
Consider it as an additional parameter!
var foo = {
value : 'a',
getResult: function(this) { //just explainatory, is NOT VALID Javascript!!!
return this.value;
}
};
function exampleMainFunction() {
var funcRef = foo.getResult;
var result1 = funcRef(); //calls like getResult(window);
var result2 = foo.getResult(); //calls like getResult(foo);
console.log(result1);
console.log(result2);
}
Bottom line
- “this” could be a DIFFERENT object from the one where is defined.
- you can even set the value of “this” programmatically! (using the “apply”
function)
- can trick many developers (especially when we are dealing with callbacks)
Part 2: new operator
Part 2: new operator
Javascript doesn't have a (natively) concept of classes, so why having a new
keyword?
in C#
you can use the new keyword ONLY for some specific methods called
constructors: those are functions that have the following restrictions:
1. Got the same name as the Class that contains the function
2. Cannot be static
3. Cannot return any value
but what's the real reason to use a constructor?
in C#
initialize an object with some specific properties and methods.
In c# those members are described inside the definition of a class.
When you call a constructor of a specific class you are doing just that: create an
object with the same exact members defined in the class definition.
in JavaScript
In Javascript we cannot (natively) define classes, but we can take advantage of
the fact that is a weakly typed language.
This allows us to describe those members inside a function.
in JavaScript
to make a very loose comparison,
a function can act as both a Class and a Constructor at the same time.
new operator in JavaScript
function Example() {
this.intMember = 1;
this.stringMember = 'abc';
this.funcMember = function(addThisToIntMember) {
this.intMember = this.intMember + addThisToIntMember;
}
}
var example = new Example();
what happens when call a function with “new”
1. a object is created (this);
2. all the members described in the prototype property of the called function are
copied to the new object
3. returns “this” (if there are no other return statements);
Quick Test
function Foo() {
var that = this;
this.value = ‘a’;
function retValueFunc() { return this.value; };
function retThatValueFunc() { return that.value; }
return { retValue : retValueFunc, retThatValue : retThatValueFunc };
}
var foo = new Foo();
var result1 = foo.retValue();
var result2 = foo.retThatValue();
Quick Test
function Foo() {
var that = this;
this.value = ‘a’;
function retValueFunc() { return this.value; };
function retThatValueFunc() { return that.value; }
return { retValue : retValueFunc, retThatValue : retThatValueFunc };
}
var foo = new Foo();
var result1 = foo.retValue();
var result2 = foo.retThatValue();
result1 == undefined
result2 == “a”
Part 3: variable hoisting & undefined value
Part 3: variable hoisting & undefined value
- the scope of a variable in javascript is in fact just related to the function where
is declared and NOT to the block.
- that’s because of a mechanism called HOISTING that makes Javascript treat
all var declarations as if they were indicated at the top of the function
- use “let” instead!!!!
Part 3: variable hoisting & undefined value
function example() {
if(true) {
var aaa = 5;
}
console.log(aaa);
}
has similar behaviour to..
Part 3: variable hoisting & undefined value
function example() {
var aaa;
if(true) {
aaa = 5;
}
console.log(aaa);
}
...this code
Part 3: variable hoisting & undefined value
- in c# there is no concept of UNDEFINED value
- UNDEFINED !== null
- NULL means “I don’t have a value”
- UNDEFINED means “I don’t know what value I got, or even if I got a value!”
in C#
int i;
Object o;
even without declaration
we know that
i == 0
o == null
(default values)
in C#
var a;
doesn’t compile at all!
(Implicitly-typed variables
must be initialized)
c# doesn’t have a concept of
undefined value
in JavaScript
var a;
a is undefined.
anything that doesn’t
exists is undefined and no
exceptions are raised at
compile time
Part 4: Inheritance
in C#
classical inheritance allows us to reuse some code on multiple classes. this
means that each object of a particular class has all the properties declared in itself
plus the members of its superclass.
in C#
class SuperClass
{
public int Number { get;set;}
public int ReturnTwice()
{
return this.Number * 2;
}
}
class SubClass : SuperClass
{
public int ReturnSquare()
{
return this.Number * this.Number;
}
}
in JavaScript
- as we already know, in Javascript to recreate a behaviour similar to classes we are
using functions.
- each function has a specific member called prototype. the value of the prototype can be
any object.
- this object is cloned inside a private member (called _proto_) that is part of any object
in Javascript.
in JavaScript
.
Function
+ prototype creates using
new operator
Object
- __proto__ (cloned from
prototype)
in JavaScript
Assuming i want the property of the object named “ccc” of “ObjectA”
ObjectA
- __proto__ = ObjectB
+ aaa = 1
Is it contained in ObjectA? nope.
ObjectB
- __proto__ = ObjectC
+ bbb = 2
ObjectC
- __proto__ = null
+ ccc= 3
in JavaScript
Assuming i want the property of the object named “ccc” of “ObjectA”
ObjectA
- __proto__ = ObjectB
+ aaa = 1
Is it contained in ObjectB? nope.
ObjectB
- __proto__ = ObjectC
+ bbb = 2
ObjectC
- __proto__ = null
+ ccc= 3
in JavaScript
Assuming i want the property of the object named “ccc” of “ObjectA”
ObjectA
- __proto__ = ObjectB
+ aaa = 1
Is it contained in ObjectC? yes.
ObjectB
- __proto__ = ObjectC
+ bbb = 2
ObjectC
- __proto__ = null
+ ccc= 3
in JavaScript
function SuperClass() {
this.numb = 3;
this.returnTwice = function() {
return this.numb * 2;
}
}
function SubClass() {
this.returnSquare = function() {
return this.numb * this.numb;
}
}
SubClass.prototype = new SuperClass();
var one = new SubClass();
one.returnSquare();
one.returnTwice();
one.numb;
one.toString();
one.aaaaaaa;
in JavaScript
function SuperClass() {
this.numb = 3;
this.returnTwice = function() {
return this.numb * 2;
}
}
function SubClass() {
this.returnSquare = function() {
return this.numb * this.numb;
}
}
SubClass.prototype = new SuperClass();
var one = new SubClass();
one.returnSquare();
one.returnTwice();
one.numb;
one.toString();
one.aaaaaaa;
returnSquare is
part of the
object, returns 9
in JavaScript
function SuperClass() {
this.numb = 3;
this.returnTwice = function() {
return this.numb * 2;
}
}
function SubClass() {
this.returnSquare = function() {
return this.numb * this.numb;
}
}
SubClass.prototype = new SuperClass();
var one = new SubClass();
one.returnSquare();
one.returnTwice();
one.numb;
one.toString();
one.aaaaaaa;
returnTwice and
numb are not part
of the object, but
part of the object
contained in
__proto__
in JavaScript
function SuperClass() {
this.numb = 3;
this.returnTwice = function() {
return this.numb * 2;
}
}
function SubClass() {
this.returnSquare = function() {
return this.numb * this.numb;
}
}
SubClass.prototype = new SuperClass();
var one = new SubClass();
one.returnSquare();
one.returnTwice();
one.numb;
one.toString();
one.aaaaaaa;
toString() is part of the
__proto__'s __proto__
member, which is
Object.prototype (the
main base class).
returns [object Object]
in JavaScript
function SuperClass() {
this.numb = 3;
this.returnTwice = function() {
return this.numb * 2;
}
}
function SubClass() {
this.returnSquare = function() {
return this.numb * this.numb;
}
}
SubClass.prototype = new SuperClass();
var one = new SubClass();
one.returnSquare();
one.returnTwice();
one.numb;
one.toString();
one.aaaaaaa;
aaaaaaa does not
exists in anyone of
the chained
__proto__
members. returns
undefined
in JavaScript
- a main difference is that superclasses are immutable on classical inheritance,
but variable on prototypal inheritance.
- nothing stops us to replace the prototype of a function with another one. in
this case, only the new instances of the object will have the new methods and
members. old ones will continue to have the older subclasses.
Q & A
Journey of a C# developer into Javascript

Mais conteúdo relacionado

Mais procurados

LinkedIn TBC JavaScript 100: Functions
 LinkedIn TBC JavaScript 100: Functions LinkedIn TBC JavaScript 100: Functions
LinkedIn TBC JavaScript 100: Functions
Adam Crabtree
 

Mais procurados (20)

Java Script Best Practices
Java Script Best PracticesJava Script Best Practices
Java Script Best Practices
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
 
4front en
4front en4front en
4front en
 
Functional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis AtencioFunctional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis Atencio
 
Java script
Java scriptJava script
Java script
 
LinkedIn TBC JavaScript 100: Functions
 LinkedIn TBC JavaScript 100: Functions LinkedIn TBC JavaScript 100: Functions
LinkedIn TBC JavaScript 100: Functions
 
PHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & ClosuresPHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & Closures
 
Anonymous functions in JavaScript
Anonymous functions in JavaScriptAnonymous functions in JavaScript
Anonymous functions in JavaScript
 
Booting into functional programming
Booting into functional programmingBooting into functional programming
Booting into functional programming
 
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD? WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
 
DIWE - Programming with JavaScript
DIWE - Programming with JavaScriptDIWE - Programming with JavaScript
DIWE - Programming with JavaScript
 
Things about Functional JavaScript
Things about Functional JavaScriptThings about Functional JavaScript
Things about Functional JavaScript
 
Frege - consequently functional programming for the JVM
Frege - consequently functional programming for the JVMFrege - consequently functional programming for the JVM
Frege - consequently functional programming for the JVM
 
Functional Javascript
Functional JavascriptFunctional Javascript
Functional Javascript
 
Frege Tutorial at JavaOne 2015
Frege Tutorial at JavaOne 2015Frege Tutorial at JavaOne 2015
Frege Tutorial at JavaOne 2015
 
Functional programming in JavaScript
Functional programming in JavaScriptFunctional programming in JavaScript
Functional programming in JavaScript
 
Functional go
Functional goFunctional go
Functional go
 
Client sidescripting javascript
Client sidescripting javascriptClient sidescripting javascript
Client sidescripting javascript
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
 
Virtual function
Virtual functionVirtual function
Virtual function
 

Semelhante a Journey of a C# developer into Javascript

Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)
Piyush Katariya
 

Semelhante a Journey of a C# developer into Javascript (20)

Javascript
JavascriptJavascript
Javascript
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
Oojs 1.1
Oojs 1.1Oojs 1.1
Oojs 1.1
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answers
 
C++ aptitude
C++ aptitudeC++ aptitude
C++ aptitude
 
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...
 
Memory Management In C++
Memory Management In C++Memory Management In C++
Memory Management In C++
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & Answers
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
 
C questions
C questionsC questions
C questions
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)
 
25-functions.ppt
25-functions.ppt25-functions.ppt
25-functions.ppt
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
JavaScript For CSharp Developer
JavaScript For CSharp DeveloperJavaScript For CSharp Developer
JavaScript For CSharp Developer
 
EcmaScript unchained
EcmaScript unchainedEcmaScript unchained
EcmaScript unchained
 
Real life-coffeescript
Real life-coffeescriptReal life-coffeescript
Real life-coffeescript
 
"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues
 

Último

Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 

Último (20)

ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 

Journey of a C# developer into Javascript

  • 1. Journey of a C# developer into Javascript Massimo Franciosa
  • 2. Permit me to introduce myself
  • 3. Permit me to introduce myself - From Napoli, Italy - Software Developer since 2008 - Worked for several companies between Italy & UK - Optima Italia - Webuild - Prama (AsianD8, AsianBride) - Sporting Index - Reading Room
  • 4. Might be useful for... - Backend developers that occasionally work on Javascript code - People that can already write in Javascript but never studied it - might also be useful for frontenders that want to understand differences the other way round (difference from JavaScript to C#)
  • 5. WON’T be useful for... - people already savvy in JavaScript and and in C# - someone that want to learn JavaScript coding best practices. the code here is just for explainatory purposes!
  • 6. Part 1: what is “this”?
  • 7. Ever wondered why? function Somefunction(){ var that = this; ... }
  • 8. Look at this code… c# public class Example { class Foo { private string _value = "a"; public string GetValue() { return this._value; } } public void ExampleMainFunction() { Foo foo = new Foo(); Func<string> funcRef = foo.GetValue; string result1 = funcRef(); string result2 = foo.GetValue(); } }
  • 9. Look at this code… c# public class Example { class Foo { private string _value = "a"; public string GetValue() { return this._value; } } public void ExampleMainFunction() { Foo foo = new Foo(); Func<string> funcRef = foo.GetValue; string result1 = funcRef(); string result2 = foo.GetValue(); } } result1 == “a” result2 == “a”
  • 10. Look at this code… JavaScript var foo = { value : 'a', getResult: function() { return this.value; } }; function exampleMainFunction() { var funcRef = foo.getResult; var result1 = funcRef(); var result2 = foo.getResult(); console.log(result1); console.log(result2); }
  • 11. Look at this code… JavaScript var foo = { value : 'a', getResult: function() { return this.value; } }; function exampleMainFunction() { var funcRef = foo.getResult; var result1 = funcRef(); var result2 = foo.getResult(); console.log(result1); console.log(result2); } result1 == “a” result2 == “a”
  • 12. Look at this code… JavaScript var foo = { value : 'a', getResult: function() { return this.value; } }; function exampleMainFunction() { var funcRef = foo.getResult; var result1 = funcRef(); var result2 = foo.getResult(); console.log(result1); console.log(result2); } result1 == “a” result2 == “a”
  • 13. Look at this code… JavaScript var foo = { value : 'a', getResult: function() { return this.value; } }; function exampleMainFunction() { var funcRef = foo.getResult; var result1 = funcRef(); var result2 = foo.getResult(); console.log(result1); console.log(result2); } result1 == undefined result2 == “a”
  • 14. Consider it as an additional parameter! var foo = { value : 'a', getResult: function(this) { //just explainatory, is NOT VALID Javascript!!! return this.value; } }; function exampleMainFunction() { var funcRef = foo.getResult; var result1 = funcRef(); //calls like getResult(window); var result2 = foo.getResult(); //calls like getResult(foo); console.log(result1); console.log(result2); }
  • 15. Bottom line - “this” could be a DIFFERENT object from the one where is defined. - you can even set the value of “this” programmatically! (using the “apply” function) - can trick many developers (especially when we are dealing with callbacks)
  • 16. Part 2: new operator
  • 17. Part 2: new operator Javascript doesn't have a (natively) concept of classes, so why having a new keyword?
  • 18. in C# you can use the new keyword ONLY for some specific methods called constructors: those are functions that have the following restrictions: 1. Got the same name as the Class that contains the function 2. Cannot be static 3. Cannot return any value but what's the real reason to use a constructor?
  • 19. in C# initialize an object with some specific properties and methods. In c# those members are described inside the definition of a class. When you call a constructor of a specific class you are doing just that: create an object with the same exact members defined in the class definition.
  • 20. in JavaScript In Javascript we cannot (natively) define classes, but we can take advantage of the fact that is a weakly typed language. This allows us to describe those members inside a function.
  • 21. in JavaScript to make a very loose comparison, a function can act as both a Class and a Constructor at the same time.
  • 22. new operator in JavaScript function Example() { this.intMember = 1; this.stringMember = 'abc'; this.funcMember = function(addThisToIntMember) { this.intMember = this.intMember + addThisToIntMember; } } var example = new Example();
  • 23. what happens when call a function with “new” 1. a object is created (this); 2. all the members described in the prototype property of the called function are copied to the new object 3. returns “this” (if there are no other return statements);
  • 24. Quick Test function Foo() { var that = this; this.value = ‘a’; function retValueFunc() { return this.value; }; function retThatValueFunc() { return that.value; } return { retValue : retValueFunc, retThatValue : retThatValueFunc }; } var foo = new Foo(); var result1 = foo.retValue(); var result2 = foo.retThatValue();
  • 25. Quick Test function Foo() { var that = this; this.value = ‘a’; function retValueFunc() { return this.value; }; function retThatValueFunc() { return that.value; } return { retValue : retValueFunc, retThatValue : retThatValueFunc }; } var foo = new Foo(); var result1 = foo.retValue(); var result2 = foo.retThatValue(); result1 == undefined result2 == “a”
  • 26. Part 3: variable hoisting & undefined value
  • 27. Part 3: variable hoisting & undefined value - the scope of a variable in javascript is in fact just related to the function where is declared and NOT to the block. - that’s because of a mechanism called HOISTING that makes Javascript treat all var declarations as if they were indicated at the top of the function - use “let” instead!!!!
  • 28. Part 3: variable hoisting & undefined value function example() { if(true) { var aaa = 5; } console.log(aaa); } has similar behaviour to..
  • 29. Part 3: variable hoisting & undefined value function example() { var aaa; if(true) { aaa = 5; } console.log(aaa); } ...this code
  • 30. Part 3: variable hoisting & undefined value - in c# there is no concept of UNDEFINED value - UNDEFINED !== null - NULL means “I don’t have a value” - UNDEFINED means “I don’t know what value I got, or even if I got a value!”
  • 31. in C# int i; Object o; even without declaration we know that i == 0 o == null (default values)
  • 32. in C# var a; doesn’t compile at all! (Implicitly-typed variables must be initialized) c# doesn’t have a concept of undefined value
  • 33. in JavaScript var a; a is undefined. anything that doesn’t exists is undefined and no exceptions are raised at compile time
  • 35. in C# classical inheritance allows us to reuse some code on multiple classes. this means that each object of a particular class has all the properties declared in itself plus the members of its superclass.
  • 36. in C# class SuperClass { public int Number { get;set;} public int ReturnTwice() { return this.Number * 2; } } class SubClass : SuperClass { public int ReturnSquare() { return this.Number * this.Number; } }
  • 37. in JavaScript - as we already know, in Javascript to recreate a behaviour similar to classes we are using functions. - each function has a specific member called prototype. the value of the prototype can be any object. - this object is cloned inside a private member (called _proto_) that is part of any object in Javascript.
  • 38. in JavaScript . Function + prototype creates using new operator Object - __proto__ (cloned from prototype)
  • 39. in JavaScript Assuming i want the property of the object named “ccc” of “ObjectA” ObjectA - __proto__ = ObjectB + aaa = 1 Is it contained in ObjectA? nope. ObjectB - __proto__ = ObjectC + bbb = 2 ObjectC - __proto__ = null + ccc= 3
  • 40. in JavaScript Assuming i want the property of the object named “ccc” of “ObjectA” ObjectA - __proto__ = ObjectB + aaa = 1 Is it contained in ObjectB? nope. ObjectB - __proto__ = ObjectC + bbb = 2 ObjectC - __proto__ = null + ccc= 3
  • 41. in JavaScript Assuming i want the property of the object named “ccc” of “ObjectA” ObjectA - __proto__ = ObjectB + aaa = 1 Is it contained in ObjectC? yes. ObjectB - __proto__ = ObjectC + bbb = 2 ObjectC - __proto__ = null + ccc= 3
  • 42. in JavaScript function SuperClass() { this.numb = 3; this.returnTwice = function() { return this.numb * 2; } } function SubClass() { this.returnSquare = function() { return this.numb * this.numb; } } SubClass.prototype = new SuperClass(); var one = new SubClass(); one.returnSquare(); one.returnTwice(); one.numb; one.toString(); one.aaaaaaa;
  • 43. in JavaScript function SuperClass() { this.numb = 3; this.returnTwice = function() { return this.numb * 2; } } function SubClass() { this.returnSquare = function() { return this.numb * this.numb; } } SubClass.prototype = new SuperClass(); var one = new SubClass(); one.returnSquare(); one.returnTwice(); one.numb; one.toString(); one.aaaaaaa; returnSquare is part of the object, returns 9
  • 44. in JavaScript function SuperClass() { this.numb = 3; this.returnTwice = function() { return this.numb * 2; } } function SubClass() { this.returnSquare = function() { return this.numb * this.numb; } } SubClass.prototype = new SuperClass(); var one = new SubClass(); one.returnSquare(); one.returnTwice(); one.numb; one.toString(); one.aaaaaaa; returnTwice and numb are not part of the object, but part of the object contained in __proto__
  • 45. in JavaScript function SuperClass() { this.numb = 3; this.returnTwice = function() { return this.numb * 2; } } function SubClass() { this.returnSquare = function() { return this.numb * this.numb; } } SubClass.prototype = new SuperClass(); var one = new SubClass(); one.returnSquare(); one.returnTwice(); one.numb; one.toString(); one.aaaaaaa; toString() is part of the __proto__'s __proto__ member, which is Object.prototype (the main base class). returns [object Object]
  • 46. in JavaScript function SuperClass() { this.numb = 3; this.returnTwice = function() { return this.numb * 2; } } function SubClass() { this.returnSquare = function() { return this.numb * this.numb; } } SubClass.prototype = new SuperClass(); var one = new SubClass(); one.returnSquare(); one.returnTwice(); one.numb; one.toString(); one.aaaaaaa; aaaaaaa does not exists in anyone of the chained __proto__ members. returns undefined
  • 47. in JavaScript - a main difference is that superclasses are immutable on classical inheritance, but variable on prototypal inheritance. - nothing stops us to replace the prototype of a function with another one. in this case, only the new instances of the object will have the new methods and members. old ones will continue to have the older subclasses.
  • 48. Q & A