gpt4 book ai didi

prototype - JavaScript 原型(prototype)错误

转载 作者:行者123 更新时间:2023-11-28 01:54:34 26 4
gpt4 key购买 nike

我一直在开发一个 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(){

有人知道我的代码出了什么问题吗?

非常感谢!

最佳答案

也许你应该在声明“tex”之后编写这个原型(prototype)?

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

更改为:

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

关于prototype - JavaScript 原型(prototype)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19317339/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com