简体   繁体   English

如何将非文字形式添加到jQuery之类的变量中?

[英]How to add non-literal into a variable like jQuery?

In JQuery at the last part of the code jQuery variable is assigned to a $ sign: 在JQuery中,代码的最后部分将jQuery变量分配给$符号:

 // Expose jQuery and $ identifiers, even in // AMD (#7102#comment:10, https://github.com/jquery/jquery/pull/557) // and CommonJS for browser emulators (#13566) if ( typeof noGlobal === strundefined ) { window.jQuery = window.$ = jQuery; } 

Now I'm making my own framework called ezo which promotes fast manipulation for DOM using id and program CSS through JavaScript file without making necessarily a CSS File and more code friendly. 现在,我正在建立自己的名为ezo的框架,该框架通过JavaScript文件使用id和程序CSS促进对DOM的快速操作,而不必使CSS文件和代码更加友好。

I did the same as JQuery in ezo (with full democ code) : 我在ezo中做了与JQuery相同的操作(带有完整的演示代码):

 var ezo = function(selector) { var dom = document.getElementById(selector); dom.setText = function(string) { dom.innerText = string; }; dom.appendText = function(string) { dom.innerText = dom.innerText + string; }; dom.setSize = function(width, height) { $(selector).css("width", width); $(selector).css("height", height); }; dom.setBGColor = function(color) { $(selector).css("background-color", color); }; dom.setFontSize = function(size) { $(selector).css("font-size", size); } dom.setFontColor = function(color) { $(selector).css("color", color); } dom.align = function(al) { $(selector).css("text-align", al); }; dom.getNodeName = function() { return $(selector)[0].nodeName; }; return document.getElementById(selector); }; $(document).ready = function() { window.ezo = window.# = ezo; }; 

Then I tested my framework on a html file to manipulate nodes: 然后,我在html文件上测试了我的框架以操作节点:

 <!DOCTYPE html> <html> <head> <title>Ezo Test</title> <link rel="stylesheet" href="stylesheet.css" /> <script src="jquery-1.11.2.js"></script> <script src="ezo-1.0.js"></script> <script src="script.js"></script> </head> <body> <h1 id="header"> This is a header! Edit Me </h1> <p id="par">I'm a lovely paragraph!</p> <div id="box"> <p id="par2">I'm an empty div !</p> </div> </body> </html> 

After I ran the html file the JS console warns me that # is an illegal token! 运行html文件后,JS控制台警告我#是非法令牌! How can I make # as a variable like ezo without having this error just like JQuery's $ sign? 我怎样才能使#作为像ezo这样的变量,而又不会像JQuery的$符号一样出现此错误呢?

How can I make # as a variable like ezo without having this error just like JQuery's $ sign? 我怎样才能使#作为像ezo这样的变量,而又不会像JQuery的$符号一样出现此错误呢?

You can't. 你不能 # isn't a valid IdentifierName character ; #不是有效的IdentifierName字符 $ is. $是。 Variables and property name literals have to contain only valid IdentifierName characters. 变量和属性名称文字应仅包含有效的IdentifierName字符。 (There's slightly more to it, because the first character has additional requirements, but see the link for details.) There are basically only two non-letter characters you can use at the beginning of a variable or property name, $ and _ . (由于第一个字符还有其他要求,因此还有更多内容,但是请参阅链接以了解详细信息。)基本上,在变量或属性名称的开头只能使用两个非字母字符,即$_ Both have been used by Well Known Libraries. 两者都已被知名图书馆使用。 :-) :-)

While you could create a window property using brackets notation and quotes: 可以使用方括号和引号创建window属性:

window["#"] = ...

...that would obviously be really awkward to use, as you would have to write window["#"] everywhere (not just # ). ...这显然很尴尬,因为您将不得不在所有地方(不仅是# )编写window["#"] :-) Or of course, you could pick any letter of the English alphabet you like, really, and just warn people what it is, but that's probably not going to be popular. :-)当然,您也可以选择任何您喜欢的英文字母,并且仅警告人们它是什么,但这可能不会流行。 Or pick a non-English letter like Ω, but A) That might not be popular in Greece, and B) It's going to be awkward to type on most keyboards. 或选择一个非英语字母,例如Ω,但A)在希腊可能并不流行,B)在大多数键盘上打字都会很尴尬。

More realistic options: 更现实的选择:

  • ezo is pretty short and easy to type, you could just use that. ezo非常简短,易于键入,您可以使用它。
  • Or you could use $ on the assumption that someone using your framework won't be using jQuery (or if they do, they can use jQuery's noConflict ). 或者你可以使用$假设有人使用你的框架不会使用jQuery(或者,如果他们这样做,他们可以使用jQuery的noConflict )。
  • Or $$ (which was also used by PrototypeJS, but that's not widely used anymore except in legacy codebases) $$ (PrototypeJS也使用了$$ ,但除旧代码库外,它已不再广泛使用)
  • Or _e _e
  • Or any of a million other things... 或其他百万种事物中的任何一种...

JavaScript object names must begin with a Unicode Letter, or underscore _, dollar sign $. JavaScript对象名称必须以Unicode字母或下划线_,美元符号$开头。 That's just how the language was defined. 这就是语言的定义方式。 jQuery has colonized the $ and various libraries like underscore.js have prior claim to _. jQuery对$进行了殖民化处理,underscore.js之类的各种库已在_之前声明了权利。 I would choose something with two characters or more, like $e or $z. 我会选择两个或更多字符的东西,例如$ e或$ z。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM