//create a generic person object
var person = {
firstName : "Aaron", //public
lastName : "Rodgers", //public
getThis : function(){
console.log(this);
},
createName : function(){
//jquery is availbe directly inside this object
//variable properties of this object need to be accessed
//through the this keyword
var nameDiv = $("<div>" + this.firstName + "</div>");
console.log(nameDiv);
//accessing a property w/o the this keyword should be an error
}
};
//call a function that uses jquery
person.createName();
//now create a person varialble using the module pattern
var person = (function(){
var firstName = "Greg";
var lastName = "Jennings";
var that = this;
var hello= $("<div>Hello</div>");
return {
getFirstName : function(){
console.log(that.firstName); //undefined becuase this referes to the return object literal//
console.log(firstName);
},
createName : function(){
var nameDiv = $("<div>" + firstName + "</div>");
console.log(nameDiv);
}
};
}());
person.getFirstName();
person.createName();
//lastly lets create a person using constructor pattern
function Person(firstName,lastName){
this.firstName = firstName;
this.lastName = lastName;
this.getFirstName = function(){
console.log(this.firstName);
};
};
var newPerson= new Person("Charles","Woodson");
newPerson.getFirstName();
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.