Reputation:
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!
Upvotes: 1
Views: 325
Reputation:
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
Upvotes: 0
Reputation:
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.
Upvotes: 0
Reputation: 489
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?!?!?!'
}
};
Upvotes: 1