patternjavascriptMinor
JavaScript OOP with virtual, private, protected and public members
Viewed 0 times
protectedjavascriptwithpublicprivatemembersandvirtualoop
Problem
Everything you need to do OOP in one neat little package without having to do all the work by hand. Almost like C++.
Here is how you would use it:
Here are a few of the members shared by all objects:
Any idea on what coul
Here is how you would use it:
var A = Object.extend("A",
{"static" :
{"counter_start" : 5 // all static propertiers and methods should
// be initialized
,"get_counter" : function () {
var cnt = this.counter_start;
this.get_counter = function () { return ++cnt; };
return cnt;
}
}
,"public" :
{"a_public_property_of_A" : ("This value does not matter. It will be"
+"initialize with undefined before the constructor is called.")
,"constructor" : function (a, other) { this.id(A.get_counter()); this.a_public_property_of_A(a); }
,"$virtual_method" : function () {return this.a_public_property_of_A(); }
,"print" : function () { console.message("The value is "+this.virtual_method()); }
}
//,"protected" : {}
,"private" :
{"id" : "does not matter" }
});
var B = A.extend("B",
{//"static" : {},
"public" :
{"base_constructor_arguments" :
function () { var arg_A = [arguments[0], "other"]; return arg_A; }
,"constructor" : function (b) {}
,"$virtual_method" : function () {return this.a_public_property_of_A() * 100; }
}
//,"protected" : {}
//,"private" : {}
});
// *
// * - Example of object instantiation:
var a = (new A(3)).constructor();
a.print();
var b = (new B(5)).constructor();
b.print();
b.$uper.print();
var old_val = b.a_public_property(); // getting a property
var new_val = b.a_public_property(1000); // setting a property
console.log("the old value is "+old_val);
console.log("the new value is "+new_val);Here are a few of the members shared by all objects:
instance_of(_class)
cast_to(_class)
dynamic_cast(_class)
type_info_name()
clone()
$uper
Any idea on what coul
Solution
This, is really really cool. It's good to see a somewhat proper implementation of Object-Oriented-Programming in Javascript.
The only small tip I really have is to add some comments. Right now, the only comment I see is the small "about", and the usage information. Preferably you should add some comments over your methods/functions, and any code blocks that may be harder to understand.
The only small tip I really have is to add some comments. Right now, the only comment I see is the small "about", and the usage information. Preferably you should add some comments over your methods/functions, and any code blocks that may be harder to understand.
Context
StackExchange Code Review Q#80457, answer score: 2
Revisions (0)
No revisions yet.