我一直在研究一个 javascript 库。这是代码:
(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);
在我尝试在我的网页上使用该库之前,这一切看起来都是不错的代码。当我尝试激活它时,我收到一条错误消息,上面写着“错误:意外令牌'。'”指的是以下tex.prototype
行:
},
tex.prototype = {
dit : function(){
有谁知道我的代码有什么问题?
太感谢了!