Introducing
Your new presentation assistant.
Refine, enhance, and tailor your content, source relevant images, and edit visuals quicker than ever before.
Trending searches
EVERYTHING in JavaScript can be evaluated to True of False!
When a function is invoked as a method on an object, then 'this' refers to the object itself.
Apply/Call
var Cat = function(name, color){
this.name = name;
this.color = color;
this.sayMyName = function(){
alert(this.name);
}
}
var myCat = new Cat('meowy', 'black');
alert(myCat.name); //meowy
myCat.sayMyName(); //meowy
'this' is what you tell it to be....
var dog = {
name : 'wolfy'
}
var sayMyName = function(){
alert(this.name);
};
sayMyName.call(dog);
When a function is executed as a function, 'this' is referring to the global window object...
var Cat = function(name, color){
this.name = name;
this.color = color;
}
var myCat = new Cat('meowy', 'black');
alert(myCat.name); //meowy
function doSomething(){
this.name = 'John'
}
doSomething();
alert(name); //John!
var foo;
if (!foo){
alert('foo is undefined!');
}
if (!bar){
// bar is undeclared!!
// This will error!
}
All these are falsey:
var x = "false";
if (x){
alert('String false is true!');
}
var f = function(){
...
}
if (f){
alert('Function is an object and an object is truthy!');
}
var dog = {
name : 'wolfy'
}
var sayMyName = function(){
alert(this.name);
};
sayMyName.call(dog);
var saySomething = function(something) {
alert(something);
}
var executeFunction = function(functionToRun){
var str = 'hi';
functionToRun(str);
}
executeFunction(saySomething);
Function is just another object ... with a few special inherited methods..
var one = 1;
var oneStr = '1';
alert(one == oneStr); // True
alert(one === oneStr); // False
JavaScript is NOT a Classical language like java ... no 'extends' .. no 'implements' .... but there is a different type of inheritance..
var DogClass = function(name){
this.name = name;
this.id = DogClass.prototype.dogCounter;
DogClass.prototype.dogCounter++;
}
DogClass.prototype = {
sayMyName : function(){
alert(this.name);
},
dogCounter: 0
}
var myDog = new DogClass('wolfy');
var myDog2 = new DogClass('dolly');
myDog.sayMyName();
myDog2.sayMyName();
alert(myDog.id);
alert(myDog2.id);