Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jun 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Class initialization && <script> loader

    Hello fellas,

    First problem
    I want to initialize class php-like
    PHP Code:
    $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
    Last edited by Kor; 06-25-2010 at 02:37 PM. Reason: inserting the proper Title: Prototype

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    Code:
    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
    Code:
    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?
    Last edited by Kor; 06-25-2010 at 01:27 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New Coder
    Join Date
    Jun 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Okay, in web.js
    I have a prototype

    PHP Code:
        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

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    New Coder
    Join Date
    Jun 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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();
    };

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    New Coder
    Join Date
    Jun 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Problem is, after adding the script containing npcscript,
    I can't do var z = new npcscript();
    It ends up undefined

    I'm using Prototype.

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •