...

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



nicooomg
06-25-2010, 11:47 AM
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 ?

Second
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,
nico

Kor
06-25-2010, 12: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){
this.newProperty=arg;
}
var myObject1=new myConstructor('foo');
var myObject2=new myConstructor('fee');
alert(myObject1.newProperty);
alert(myObject2.newProperty);

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(){
this.newProperty='foo';
}
var i=1;
var name='myConstructor'+i;
var h=new window[name];
alert(h.newProperty);


Is this what you want?

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

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


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

// 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

Kor
06-25-2010, 12: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?

nicooomg
06-25-2010, 01:05 PM
var npcscript = Class.create();
npcscript.prototype.initialize = function(parentclass) {
var npc = this, cm = parentclass;
var html = [];
alert('klasss');
};
npcscript.prototype.run = function() {
alert('hi');
};
npcscript.prototype.close = function() {
cm.close();
};

Kor
06-25-2010, 01: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.

nicooomg
06-25-2010, 01: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.

Kor
06-25-2010, 01: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. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum