(function() {
console.log(this); // this = Window
})();
(() => console.log(this))(); // this = window
// Person Constructor
function Person(first, last) {
this.first_name = first;
this.last_name = last;
}
// New method for Person
Person.prototype.name = function() {
return this.first_name + ' ' + this.last_name; // this = Person
};
// Never use arrow functions when adding method
Person.prototype.name2 = () => this.first_name + " " + this.last_name; // this = Window
// However, you should use it if you want to preserve the context inside
Person.prototype.career = function(array) {
// this = Person
return array.map(function(a) {
return this.first_name + ' ' + a; // this = Window
});
};
Person.prototype.career2 = function(array) {
return array.map(a => this.first_name + ' worked at ' + a); // this = Person
};
// Open the console to see the results
var ed = new Person('Edward', 'Snowden');
console.log(ed.name());
console.log(ed.name2());
console.log(ed.career(['CIA', 'NSA']));
console.log(ed.career2(['CIA', 'NSA']));
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.