1

私は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);

Web ページでライブラリを使用しようとするまでは、すべてが適切なコードのように見えます。tex.prototypeアクティブ化しようとすると、次の行を参照して、「エラー: 予期しないトークン '.'」というエラーが表示されます。

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

私のコードの問題が何であるかを誰かが知っていますか?

どうもありがとう!

4

3 に答える 3

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
于 2013-10-11T12:16:23.197 に答える
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これは、キーワードの設定が許可されていないことを意味します。

于 2013-10-11T11:49:12.277 に答える