View Full Version : Prototype Class initialization && <script> loader

06-25-2010, 12:47 PM
Hello fellas,

First problem
I want to initialize class php-like

$i = 1;
$name = 'myClass' . $i;
$h = new $name();

How to do it in javascript ?

If I'm adding a <script> to <head>, and in this new script there's a class named npcscript.
How come right after adding the element, I can't initialize the class?

Thanks for your time,

06-25-2010, 01:20 PM
It is not very clear what do you mean by class, because JavaScript is not a class based language, it is a prototype based language. That means the inheritance is performed via a process of cloning existing objects - on using so called prototypes. In other words JavaScript uses the delegation (from object to its prototype), not the inheritance. We may say that Javascript is able to simulate many class-based-like features, but on using prototypes instead of classes. Javascript simply uses objects, not classes and instances. Everything is an object in javascript: variables, references, functions, methods...

Objects may be created in several ways in javascript. To keep the parallel with Your PHP example, you may use a constructor

function myConstructor(arg){
var myObject1=new myConstructor('foo');
var myObject2=new myConstructor('fee');

Now: Your first problem. If I well understand you want to refer dynamically a constructor. Well, you may use the fact that Globals (variables and functions) are all properties of the window "Ultimate" Global Object

function myConstructor1(){
var i=1;
var name='myConstructor'+i;
var h=new window[name];

Is this what you want?

I don't understand your second problem. Can you detail it?

06-25-2010, 01:37 PM
Okay, in web.js
I have a prototype

prepair: function() {
$('npc-avatar-img').setAttribute('src', this._avatar.src);
// var f = new element ( script ... );
// append to header
run: function() {

// this._var = new npcscript(this);

the script i'm adding, contains the class named npcscript.
I checked in Opera Dragonfly, the script is indeed added to the header, but I can never initialize nocscript :s

06-25-2010, 01:43 PM
Once again: JavaScript has no classes. JavaScript works with objects and functions. Methods are also functions, but defined as properties of some objects.

Moreover, what you have posted is not native javascript, it is a framework, thus it is hard for us to guess what and where all those methods and constructors (not classes) were defined.

What is npcscript? Where it is defined? Is it an independent function, or a method/propery of another object?

06-25-2010, 02:05 PM
var npcscript = Class.create();
npcscript.prototype.initialize = function(parentclass) {
var npc = this, cm = parentclass;
var html = [];
npcscript.prototype.run = function() {
npcscript.prototype.close = function() {

06-25-2010, 02:15 PM
And were is the Class object? And what the create() custom method does?

Working with JavaScript frameworks/libraries is an impossible job without knowing the "legend" of the custom functions and methods defined by that particular library/framework.

06-25-2010, 02:20 PM
Problem is, after adding the script containing npcscript,
I can't do var z = new npcscript();
It ends up undefined

I'm using Prototype.

06-25-2010, 02:36 PM
OK, I'll move this Thread to the proper Forum: JavaScript frameworks where I hope you will find some assistance from somebody who worked with the prototype framework.

For me - I never use frameworks/libraries. They provoke more damage and confusion compared with the small advantages they might have. :)