我一直在研究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(){
有谁知道我的代码是什么问题?
非常感谢你!
答案 0 :(得分:1)
也许你应该在声明' tex'?
之后编写这个原型 },
tex.prototype = {
dit : function(){
this.innerHTML = 'Hi?!?!?!'
}
};
更改为:
};
tex.prototype = {
dit : function(){
this.innerHTML = 'Hi?!?!?!'
}
};
答案 1 :(得分:0)
var o = {}; // right
var o.a = 1; // wrong
o.a = 1; // right
替换逗号:
};
tex.prototype = {
dit : function(){
this = ...;
可能还有其他问题:
this = 1; // ReferenceError: Invalid left-hand side in assignment
这意味着您无权设置this
关键字。
答案 2 :(得分:0)
作为对我们之前讨论的回复,这是一个快速修订:
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
因此,此代码向this
添加了一个新属性:
this[0] = elem;
虽然这个崩溃了:
this = elem; // ReferenceError: Invalid left-hand side in assignment