1

I have been working on a javascript library. Here is the code:

(function (window) {
        var regex = {
            Id : /^[#]\w+$/,
            Class : /^[.]\w+$/,     
            Tag : /^\w+$/,
            validSelector : /^([#]\w+|[.]\w+|\w+)$/
        },  
        tex = function(selector){
            //only some of the functions need to select an element
            //EX:
            // style: tex(selector).style(style);
            //one that would not need a selector is the random number function:
            // tex().random(from,to);
            if (selector){
                if (typeof selector === 'string'){
                    var valid = validSelector.test(selector);
                    if( valid ){
                        if(regex.Id.test(string)){ 
                            this = document.getElementById(selector);
                        }
                        if(regex.Class.test(string)){ 
                            this = document.getElementByClass(selector);
                        }
                        if(regex.Tag.test(string)){ 
                            this = document.getElementByTagName(selector);
                        }
                    }
                }else if(typeof selector === 'object'){
                    this = selector;
                }
                //this = document.querySelector(selector);
                // I could make a selector engine byt I only need basic css selectors.
            }
        },
        tex.prototype = {
            dit : function(){
                this.innerHTML = 'Hi?!?!?!'
            }
        };
        window.tex = tex;
})(window);

It all looks like good code until I try to use the library on my webpage. When I try to activate it I get an error that says, "Error: Unexpected token '.'" referring to the tex.prototype line:

},
tex.prototype = {
    dit : function(){

Does anyone know what the matter with my code is?

Thank you so much!

3 Answers 3

1

Maybe you should write this prototype after declaring 'tex'?

    },
    tex.prototype = {
        dit : function(){
            this.innerHTML = 'Hi?!?!?!'
        }
    };

change to:

};
tex.prototype = {
    dit : function(){
        this.innerHTML = 'Hi?!?!?!'
    }
};
Sign up to request clarification or add additional context in comments.

Comments

0
var o = {};  // right
var o.a = 1; // wrong
o.a = 1;     // right

Replace the comma :

};
tex.prototype = {
    dit : function(){

There might be another problem with this = ...; :

this = 1; // ReferenceError: Invalid left-hand side in assignment

It means that you're not allowed to set the this keyword.

9 Comments

Ok, but jQuery does it that way... And the error is a couple lines of code below that.
@TekiTech "but jQuery does it that way...". Are you sure? I have just checked the sources and found nothing about that.
I have been reviewing the jQuery code and yes, it does declare this = element
@TekiTech Which version? I checked this one : code.jquery.com/jquery-1.10.2.js.
@TekiTech Same as mine... So, who's blind?
|
0

As a reply to our previous discussion, here is a quick revision :

var o;    // defines a new variable named "o"
o = {};   // sets the variable "o" as an empty object
o[0] = 1; // adds a property named "0" to this object

So, this code adds a new property to this :

this[0] = elem;

While this one crashes :

this = elem; // ReferenceError: Invalid left-hand side in assignment

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.