Newbie Problem:Use Ext.extend,but different result In IE and Firefox

March 16th, 2010
  • Dear All:
    Thanks for taking time to read my problem,below is the code

    line1: Ext.namespace('MySpace.js','MySpace.js.util');
    line2: MySpace.js.util.Commands=function(){
    line3: MySpace.js.util.Commands.superclass.constructor.ca ll(this, Array);
    line4: this.name="MySpace.js.util.Commands";
    line5: this.removeAll=function(){
    line6: while(this.length > 0){
    line7: this.pop();
    line8: }
    line9: };
    line10: };
    line11: Ext.extend(MySpace.js.util.Commands,Array);
    line12: var cmdStk=new MySpace.js.util.Commands();
    line13:
    line14: function doPushNow(item2push){
    line15: cmdStk.push( item2push );
    line16: cmdStk.push( item2push +'xxxx');
    line17: alert("pup after doPushNow ,element is :"+cmdStk.pop());
    line18: };

    this code works fine in firefox, in line18,shows correct popout element.toString() infor,
    but if run it by IE,line18 will shows "pup after doPushNow ,element is :undefiend";
    If I change line12 to "var cmdStk=new Array();",it works properly in IE and Firefox(of course,I can not use the removeAll function then)
    Could any one tell me what did I go wrong? and why?
    Thanks million to your kindly reply


  • Dear hendricd:
    Thanks for your help,it really works
    but I am really newbie to javascricpt,according to API of Ext.extend,I regards this function is just only for "over write",not for providding new function,thanks for your information.
    besides, when should I use "return this" ?
    forgive me for asking such stupid question, and any other resource for learning to build the concept ?
    Thanks for your advice again.

    Regards

    john


  • @neoart -- Since your class extends Array, your 'this' (after instantiation) is an Array. So, within the class definition you are adding function prototype methods that act on 'this'. Your method need only return this if it's appropriate for the task.

    As an example, let's add some more methods to your new Array class:


    Ext.extend(MySpace.js.util.Commands, Array, {
    removeAll : function(){
    this.length = 0;
    return this;
    },

    first: function() {
    return this[0]; //return the first element of this array
    },

    last: function() {
    return this[this.length - 1]; //return the last
    },

    clone: function() {
    return .concat(this); //make a copy of this Array instance and return it.
    }
    });


  • In your case, you are trying to extend your class with another and add a new prototype:
    Try this:

    Ext.namespace('MySpace.js.util');
    MySpace.js.util.Commands=function(){
    MySpace.js.util.Commands.superclass.constructor.ap ply(this, arguments);
    this.name="MySpace.js.util.Commands";
    };
    Ext.extend(MySpace.js.util.Commands, Array, {
    removeAll : function(){
    this.length = 0;
    return this;
    }
    });







  • #If you have any other info about this subject , Please add it free.#
    Your name:
    E-mail:
    Telphone:

    Your comments:


    If you have any other info about Newbie Problem:Use Ext.extend,but different result In IE and Firefox , Please add it free.