SlideShare a Scribd company logo
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

More Related Content

PPTX
Virtual function
PPTX
virtual function
PDF
JavaScript Core
PPT
Advanced Javascript
PDF
Functional JavaScript Fundamentals
PPT
Advanced JavaScript
PDF
Advanced javascript
PDF
JavaScript Functions
Virtual function
virtual function
JavaScript Core
Advanced Javascript
Functional JavaScript Fundamentals
Advanced JavaScript
Advanced javascript
JavaScript Functions

What's hot (20)

PDF
Java Script Best Practices
PDF
(3) cpp procedural programming
PPTX
4front en
PPTX
Functional Programming in JavaScript by Luis Atencio
PPTX
Java script
PPTX
LinkedIn TBC JavaScript 100: Functions
PPT
PHP 5.3 Part 2 - Lambda Functions & Closures
PDF
Anonymous functions in JavaScript
PDF
Booting into functional programming
PPTX
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
PPSX
DIWE - Programming with JavaScript
PPTX
Things about Functional JavaScript
PDF
Frege - consequently functional programming for the JVM
PDF
Functional Javascript
PDF
Frege Tutorial at JavaOne 2015
PPTX
Functional programming in JavaScript
PDF
Functional go
PDF
Client sidescripting javascript
PPTX
Intro to Javascript
DOCX
Virtual function
Java Script Best Practices
(3) cpp procedural programming
4front en
Functional Programming in JavaScript by Luis Atencio
Java script
LinkedIn TBC JavaScript 100: Functions
PHP 5.3 Part 2 - Lambda Functions & Closures
Anonymous functions in JavaScript
Booting into functional programming
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
DIWE - Programming with JavaScript
Things about Functional JavaScript
Frege - consequently functional programming for the JVM
Functional Javascript
Frege Tutorial at JavaOne 2015
Functional programming in JavaScript
Functional go
Client sidescripting javascript
Intro to Javascript
Virtual function
Ad

Similar to Journey of a C# developer into Javascript (20)

KEY
Javascript tid-bits
PPTX
Javascript Objects Deep Dive
PDF
JavaScript Essentials
PDF
Javascript
ODP
Javascript
PPTX
Ajaxworld
PPTX
Object Oriented Javascript part2
PDF
Js in-ten-minutes
PPT
JavaScript - Programming Languages course
PDF
JavaScript For CSharp Developer
PPTX
Object oriented javascript
PPT
Douglas Crockford Presentation Goodparts
PPT
Beginning Object-Oriented JavaScript
PPT
Advanced Javascript
PPT
Advanced Javascript
PPTX
JavaScript OOPS Implimentation
PDF
Let's JavaScript
PPTX
WEB222-lecture-4.pptx
PPT
Java script unleashed
PDF
OOPS JavaScript Interview Questions PDF By ScholarHat
Javascript tid-bits
Javascript Objects Deep Dive
JavaScript Essentials
Javascript
Javascript
Ajaxworld
Object Oriented Javascript part2
Js in-ten-minutes
JavaScript - Programming Languages course
JavaScript For CSharp Developer
Object oriented javascript
Douglas Crockford Presentation Goodparts
Beginning Object-Oriented JavaScript
Advanced Javascript
Advanced Javascript
JavaScript OOPS Implimentation
Let's JavaScript
WEB222-lecture-4.pptx
Java script unleashed
OOPS JavaScript Interview Questions PDF By ScholarHat
Ad

Recently uploaded (20)

PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPT
Chapter 6 Design in software Engineeing.ppt
PPTX
Geodesy 1.pptx...............................................
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Simulation of electric circuit laws using tinkercad.pptx
PPTX
Internship_Presentation_Final engineering.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
24AI201_AI_Unit_4 (1).pptx Artificial intelligence
PDF
ETO & MEO Certificate of Competency Questions and Answers
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
“Next-Gen AI: Trends Reshaping Our World”
DOCX
573137875-Attendance-Management-System-original
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Structs to JSON How Go Powers REST APIs.pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Chapter 6 Design in software Engineeing.ppt
Geodesy 1.pptx...............................................
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Model Code of Practice - Construction Work - 21102022 .pdf
Simulation of electric circuit laws using tinkercad.pptx
Internship_Presentation_Final engineering.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
24AI201_AI_Unit_4 (1).pptx Artificial intelligence
ETO & MEO Certificate of Competency Questions and Answers
Lesson 3_Tessellation.pptx finite Mathematics
“Next-Gen AI: Trends Reshaping Our World”
573137875-Attendance-Management-System-original
Embodied AI: Ushering in the Next Era of Intelligent Systems
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...

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