correct OOP in JavaScript + console shall print object's constructor name

Arcadester

New Member
I followed John Resig's approach for creating Classes in JavaScript and extending them.\[code\]var Runtime = Class.extend({ init: function() { console.log("new Runtime"); } , attribute: null});var run = new Runtime();console.log(run);\[/code\]But in the Console the printed out object is shown as "Class" rather than as "Runtime":\[code\]Class { init=function(), attribute.... }\[/code\]which applies for all chained prototypes, when extending. Whereas:\[code\]function Runtime() {};Runtime.prototype.init = function() { ... }Runtime.prototype.attribute = nullvar run = new Runtime();console.log(run);\[/code\]in the Console results in:\[code\]Runtime { init=function(), attribute.... }\[/code\]And when using this (JavaScript Patterns by Stoyan Stefanov, page 127):\[code\]function inherit(C, P) { P = P || Object; var X = function () {}; X.prototype = P.prototype; C.prototype = new X(); C.prototype.uber = P.prototype; C.prototype.constructor = C;}\[/code\]it does print out "Runtime" again in Console. In Firebug (FF) the .prototype is of type "Runtime" too. But Chrome shows .prototype as X (from the X-function in "inherit(C, P);").I just thought, that i will make developing much easier to see in the console, what type of class i am just printing to console. In my case it can be many different classes of unpredictable type.So my questions are:[*]did i implement John Resig's approach wrong? or is it meant to not show class names in console?[*]i think the class-"name" shown in the console relates to the object's constructor method, correct? thats why Stoyan's method does what i want.[*]so, main question, why does Firefox have "Runtime" and prototype, and Firefox has "X"?[*]and, is it possible to have a constructor method in a namespace, and thereby in for example My.namespace.Runtime? to me it looks like you need to type "function ClassName()..." in order to have it work. which would prevent me from using namespaces.
 
Back
Top