// Javascript
function Animal(foodType){
this.foodType = foodType;
}
Animal.prototype.feed = function () {
alert("Fed the animal: "+ this.foodType);
};
function Cow(color){
this.color = color;
}
// Inheritance Magic is here chain one constructor to another
// call new animal first ant then fill in other data.
Cow.prototype = new Animal("Hay");
var c = new Cow("White/Black");
c.feed();
var test1 = c instanceof Animal; // True
var test2 = c instanceof Cow; // true
var a = new Animal("None");
a.feed(); // "None"
// this test is instanceof object
var test = a instanceof Animal; // true
alert(test);
function Customer(name, company){
this.name = name;
this.company= company;
// non-public (e.g private)
var mailserver = "mail.google.com";
this.sendEmail = function(email){
// sendMailViaServer(mailserver);
// alert(email);
alert(mailserver);
};
}
function AnotherCustomer(name, company) {
this.name = name;
this.company = company;
}
AnotherCustomer.prototype.send = function(email)
{
// alert("email "+email);
};
function NewCustomer(name, company){
var _namefirst = name;
var _company = company;
// create read only or write only properties
// also if you are doing something else in the setter;
Object.defineProperty(this, "name",{
get:function(){return _namefirst;}
});
Object.defineProperty(this, "company",{
get:function(){return _company;},
set:function(value){_company= value;}
});
}
// works no access to private/member data
AnotherCustomer.prototype.mailServer = "webmail.brinkster.com";
AnotherCustomer.prototype.sendMail = function(msg)
{
alert(msg);
var svr = this.mailServer;
};
// instance data and static date
console.clear();
var another = new AnotherCustomer("Larry", "mycustomer");
another.send("[email protected]"); // this works;
another.sendMail("Hey buddy");
var cust=...
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.