คำหลัก JavaScript ใด (ชื่อฟังก์ชั่นตัวแปร ฯลฯ ) ถูกสงวนไว้?
คำหลัก JavaScript ใด (ชื่อฟังก์ชั่นตัวแปร ฯลฯ ) ถูกสงวนไว้?
คำตอบ:
เราควรเชื่อมโยงไปยังแหล่งข้อมูลที่แท้จริงแทนที่จะเป็นเพียง google hit อันดับต้น ๆ
http://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Reserved_Words
JScript 8.0: http://msdn.microsoft.com/en-us/library/ttyab5c8.aspx
นี่คือบทกวีของฉันซึ่งรวมถึงคำหลักที่สงวนไว้ทั้งหมดใน JavaScript และทุ่มเทให้กับผู้ที่ยังคงซื่อสัตย์ในขณะนี้และไม่เพียง แต่พยายามทำคะแนน:
Let this long package float,
Goto private class if short.
While protected with debugger case,
Continue volatile interface.
Instanceof super synchronized throw,
Extends final export throws.
Try import double enum?
- False, boolean, abstract function,
Implements typeof transient break!
Void static, default do,
Switch int native new.
Else, delete null public var
In return for const, true, char
…Finally catch byte.
เพื่อเสริมคำตอบของ Bencดูมาตรฐาน ECMA-262 คำเหล่านี้เป็นคำสงวนอย่างเป็นทางการ แต่มีเพียงคนอวดรู้ที่ละเว้นการปฏิบัติตามมาตรฐาน สำหรับคำสงวนของการใช้งานที่เป็นที่นิยมมากที่สุดนั่นคือ firefox และ internet explorer ดูคำตอบของ benc
คำที่สงวนไว้ใน EMCAScript-262 คือคำสำคัญ s, คำที่สงวนไว้ในอนาคต , NullLiteralและBooleanLiteral s โดยที่คำหลักนั้น
break do instanceof typeof
case else new var
catch finally return void
continue for switch while
debugger function this with
default if throw
delete in try
สงวนไว้ในอนาคตของ Word s มี
abstract export interface static
boolean extends long super
byte final native synchronized
char float package throws
class goto private transient
const implements protected volatile
double import public
enum int short
NullLiteralคือ
null
และBooleanLiteralคือ
true
false
letที่นี่ แต่ฉันเห็นใน docu: ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
ฉันเพิ่งอ่านเรื่องนี้ในJavaScript & jQuery: คู่มือที่หายไป :
ไม่ใช่คำที่สงวนไว้ทั้งหมดจะทำให้เกิดปัญหาในเบราว์เซอร์ทั้งหมด แต่เป็นการดีที่สุดที่จะหลีกเลี่ยงชื่อเหล่านี้เมื่อตั้งชื่อตัวแปร
คำหลัก
break, case, catch, continue, debugger, default, delete, do, else, false, finally, for, function, if, in, instanceof, new, null, return, switch, this, throw, true, try, typeof, var, void, while, withJavaScript:
abstract, boolean, byte, char, class, const, double, enum, export, extends, final, float, goto, implements, import, int, interface, let, long, native, package, private, protected, public, short, static, super, synchronized, throws, transient, volatile, yieldสงวนไว้สำหรับการใช้งานในอนาคต:
alert, blur, closed, document, focus, frames, history, innerHeight, innerWidth, length, location, navigator, open, outerHeight, outerWidth, parent, screen, screenX, screenY, statusbar, windowที่กำหนดไว้ล่วงหน้าตัวแปรระดับโลกในเบราว์เซอร์:
alertมีการเริ่มต้นแล้ว แต่ไม่มีอะไรทำให้คุณไม่สามารถกำหนดใหม่alert = 5ได้ อย่างไรก็ตามคุณไม่สามารถตั้งค่าwindowเป็น 5 แต่คุณสามารถใช้มันเป็นตัวแปรท้องถิ่น nullนั่นเป็นไปไม่ได้กับคำหลักที่สงวนไว้ใช้ในอนาคต false, true,
นี่คือวิธีที่ไม่เชื่อเรื่องพระเจ้าของเบราว์เซอร์และภาษาในการตรวจสอบว่าสตริงเฉพาะจะถือว่าเป็นคำหลักโดยเครื่องมือ JavaScript ให้เครดิตกับคำตอบนี้ซึ่งเป็นแกนหลักของการแก้ปัญหา
function isReservedKeyword(wordToCheck) {
var reservedWord = false;
if (/^[a-z]+$/.test(wordToCheck)) {
try {
eval('var ' + wordToCheck + ' = 1');
} catch (error) {
reservedWord = true;
}
}
return reservedWord;
}
evalเพื่ออะไรก็ตามเป็นไปได้ว่าคุณทำผิด
ไม่มีคำตอบปัจจุบันที่เตือนว่าไม่คำนึงถึง ES-Dialect เบราว์เซอร์มักจะมีรายการคำหลักที่สงวนไว้ของตนเองวิธีการและอื่น ๆ ที่อยู่เหนือสิ่งที่ ES สั่ง
ตัวอย่างเช่น IE9 ห้ามมิให้ใช้ชื่อโลจิคัลเช่น: addFilter, removeFilter(เป็นชื่อวิธีสำรอง)
ดูhttp://www.jabcreations.com/blog/internet-explorer-9สำหรับรายการ 'ที่เป็นที่รู้จักกันมากในปัจจุบัน' ที่เฉพาะเจาะจงสำหรับ IE9 ฉันยังพบการอ้างอิงอย่างเป็นทางการกับพวกเขาใน msdn (หรือที่อื่น ๆ )
นี่คือรายการจากหนังสือ Eloquent JavaScript:
breakcasecatchclassconstcontinuedebuggerdefaultdeletedoelseenumexportextendfalsefinallyforfunctionifimplementsimportininstanceofinterfaceletnewnullpackageprivateprotectedpublicreturnstaticsuperswitchthisthrowtruetrytypeofvarvoidwhilewithyieldคำตอบของ benc นั้นยอดเยี่ยม แต่สำหรับสองเซ็นต์ของฉันฉันชอบหน้า w3schools 'ในนี้:
http://www.w3schools.com/js/js_reserved.asp
นอกเหนือจากการแสดงรายการคำหลักที่สงวนไว้ตามมาตรฐานแล้วยังมีรายการคำหลักที่ยาวที่คุณควรหลีกเลี่ยงในบางบริบท ตัวอย่างเช่นไม่ใช้ชื่อalertเมื่อเขียนรหัสเพื่อให้ทำงานในเบราว์เซอร์ มันช่วยให้ฉันทราบว่าเหตุใดคำบางคำจึงถูกเน้นเป็นคำหลักในเครื่องมือแก้ไขแม้ว่าฉันจะรู้ว่าไม่ใช่คำหลักก็ตาม