Skip to content
Akin C edited this page Oct 12, 2018 · 30 revisions

Welcome to the Javascript-prototype-and-instance- wiki!

This repository is part of a larger project!

◀️ PREVIOUS PROJECT| NEXT PROJECT ▶️


Javascript gives the option to inherit properties and methods by prototype. A simple example follows:

//Serves as "class" like known e.g. in C++ or Java
function myClass()
{}

//Now a property for "myClass" is defined 
//Name of property does not have to be "Property"
myClass.prototype.Property = "House";

//Now a method for "myClass" is defined
//Name of method does not have to be "Method"
myClass.prototype.Method = function()
{
	return "Blueprint for House";
}

//An instance of "myClass"
var Child = new myClass();


//Outputs string value "House"
console.log(Child.Property);

//Outputs string value "Blueprint for House"
console.log(Child.Method());

In the listing above an instance of myParent is done with the keyword new.

Every instance has its own properties and methods. A change from one instance should not influence the properties and methods from another instance. For clarification an example follows:

//Serves as "class" like in C++ or Java
function myClass()
{}

//Now a property for "myClass" is defined
myClass.prototype.Property = "House";


//Two Instances
var ChildOne = new myClass();
var ChildTwo = new myClass();

//Changing Property of "ChildOne"
ChildOne.Property = 42;

//Outputs 42
console.log(ChildOne.Property);

//Outputs the string value "House", 
//because the change is only done within instance "ChildOne"
console.log(ChildTwo.Property);

🥇 TODO: Complete and revise table content - STILL IN WORK!

Nr Code Console Description
01
function myClass()
{
    this.name = "Gandalf";
}

//myClass.prototype.name = "Arwen"

var ChildOne = new myClass();
var ChildTwo = new myClass();

ChildTwo.name = "Bilbo";

console.log(ChildOne);
console.log(ChildTwo);
console.dir(myClass);
                    
STILL IN WORK! //----CONCLUSION:
02
function myClass()
{
    //this.name = "Gandalf";
}

myClass.prototype.name = "Arwen"

var ChildOne = new myClass();
var ChildTwo = new myClass();

ChildTwo.name = "Bilbo";

console.log(ChildOne);
console.log(ChildTwo);
console.dir(myClass);
                    
STILL IN WORK! //----CONCLUSION: constructor??!

Relations

Nr Code Console Grafic
01 ---- ------- ------
02 ---- ------- ------
03 ---- ------- ------
-- ---- ------- ------

Content

STILL IN WORK!

Clone this wiki locally