-
Notifications
You must be signed in to change notification settings - Fork 0
Home
This repository is part of a larger 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 table - STILL IN WORK!
Nr | Code | Console | Description |
---|---|---|---|
01 |
function myClass()
{
this.name = "Gandalf";
}
var ChildOne = new myClass();
var ChildTwo = new myClass();
ChildTwo.name = "Bilbo";
console.log(ChildOne);
console.dir(ChildOne);
console.dir(myClass);
|
STILL IN WORK! Placeholder!!! | STILL IN WORK! //----CONCLUSION: name from myClass is inherited, but as an own property |
Nr | Code | Console | Grafic |
---|---|---|---|
01 | ---- | ------- | ------ |
02 | ---- | ------- | ------ |
03 | ---- | ------- | ------ |
-- | ---- | ------- | ------ |
STILL IN WORK!
This knowledge was gained:
-
Effective JavaScript "68 Specific Ways to Harness the Power of JavaScript" by David Herman
-
Javascript Prototype inheritance Explained ( tutorial Part1) by techsith
Sources:
-
What's the difference between console.dir and console.log? asked by Drew Noakes and answered by apsillers