This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Constructor Function | |
var Friend = function(name, password, interests, job){ | |
this.fullName = name; | |
this.password = password; | |
this.interests = interests; | |
this.job = job; | |
}; | |
function sayHello(){ | |
// uncomment the console.log to see the object that 'this' points to |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using the Dog prototype, create a new instance | |
var chester = new Dog('beagle', 'Chester', ['Gracie', 'Josey', 'Barkley']); | |
chester.intro(); // returns Hi, my name is Chester and I'm a beagle | |
console.log(chester); // returns Dog {breed: "beagle", name: "Chester", friends: Array(3), intro: ƒ} | |
// Here's another example: | |
var City = function(city, state) { | |
this.city = city || "Phoenix"; | |
this.state = state || "AZ"; | |
this.sentence = function() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var str = new String('Hello world'); | |
/******* | |
You could do the above, but it's best to avoid it (instead do like the variable str2 below) | |
(because JavaScript knows that anything inside single or double quotes has the type of String) | |
Same goes for other primitives. This is for example purposes only. | |
NOTE: To clarify -- the only time I ever use the new keyword in practice is when I use a function constructor and create my own object type. | |
*******/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function test() { | |
'use strict'; | |
return this; | |
} | |
console.log( test() ); | |
// returns undefined, not the Window object … interesting |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function test() { | |
console.log('hello world'); | |
console.log(this); | |
} | |
test(); | |
// hello world | |
// Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dog = { | |
name: 'Chester', | |
breed: 'beagle', | |
intro: function(){ | |
console.log(this); | |
} | |
}; | |
dog.intro(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dog = { | |
breed: 'Beagles', | |
lovesToChase: 'rabbits' | |
}; | |
function chase() { | |
console.log(this.breed + ' loves chasing ' + this.lovesToChase + '.'); | |
// console.log(this); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Dog(breed, name, friends){ | |
this.breed = breed; | |
this.name = name; | |
this.friends = friends; | |
this.intro = function() { | |
console.log(`Hi, my name is ${this.name} and I’m a ${this.breed}`); | |
return this; | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var objReg = { | |
hello: function() { | |
return this; | |
} | |
}; | |
var objArrow = { | |
hello: () => this | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log(this); | |
// returns the Window object | |
// Window { postMessage: ƒ, | |
// blur: ƒ, | |
// focus: ƒ, | |
// close: ƒ, | |
// frames: Window, …} | |
function myFunction() { |
NewerOlder