การส่งออกและการส่งออก
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
วัตถุประสงค์ของโปรแกรมฝึกงาน
Extern คือการประกาศที่บอก Closure Compiler ถึงชื่อของ
สัญลักษณ์ที่ไม่ควรเปลี่ยนชื่อในระหว่างการคอมไพล์ขั้นสูง
เราเรียกสัญลักษณ์เหล่านี้ว่า "ภายนอก" เนื่องจากสัญลักษณ์เหล่านี้มักจะกำหนดโดย
โค้ดภายนอกการคอมไพล์ เช่น โค้ดดั้งเดิม หรือไลบรารีของบุคคลที่สาม
ด้วยเหตุนี้ ไฟล์ภายนอกจึงมักจะมีคำอธิบายประกอบประเภทด้วย
เพื่อให้ Closure Compiler ตรวจสอบประเภทการใช้สัญลักษณ์เหล่านั้นได้
โดยทั่วไปแล้ว คุณควรคิดว่า Externs เป็นสัญญา API ระหว่าง
ผู้ใช้และผู้บริโภคของโค้ดที่คอมไพล์แล้ว
externs จะกำหนดสิ่งที่ผู้ใช้สัญญาว่าจะจัดหาให้ และสิ่งที่
ผู้บริโภคสามารถใช้ได้ ทั้ง 2 ฝ่ายต้องมีสำเนาสัญญา
Externs จะคล้ายกับไฟล์ส่วนหัวในภาษาอื่นๆ
ไวยากรณ์ Externs
Externs คือไฟล์ที่มีลักษณะคล้ายกับ JavaScript ปกติที่
มีคำอธิบายประกอบสำหรับ Closure Compiler ความแตกต่างหลักๆ ก็คือเนื้อหาของตัวแปรเหล่านี้จะไม่ได้รับการพิมพ์
เป็นส่วนหนึ่งของเอาต์พุตที่คอมไพล์ ดังนั้นค่าทั้งหมดจึงไม่มีความหมาย
มีเพียงชื่อและประเภทเท่านั้น
ด้านล่างนี้เป็นตัวอย่างไฟล์ Externs สำหรับไลบรารีอย่างง่าย
// The `@externs` annotation is the best way to indicate a file contains externs.
/**
* @fileoverview Public API of my_math.js.
* @externs
*/
// Externs often declare global namespaces.
const myMath = {};
// Externs can declare functions, most importantly their names.
/**
* @param {number} x
* @param {number} y
* @return {!myMath.DivResult}
*/
myMath.div = function(x, y) {}; // Note the empty body.
// Externs can contain type declarations, such as classes and interfaces.
/** The result of an integer division. */
myMath.DivResult = class {
// Constructors are special; member fields can be declared in their bodies.
constructor() {
/** @type {number} */
this.quotient;
/** @type {number} */
this.remainder;
}
// Methods can be declared as usual; their bodies are meaningless though.
/** @return {!Array<number>} */
toPair() {}
};
// Fields and methods can also be declared using prototype notation.
/**
* @override
* @param {number=} radix
*/
myMath.DivResult.prototype.toString = function(radix) {};
ธงของ --externs
โดยทั่วไปแล้ว คำอธิบายประกอบ @externs
เป็นวิธีที่ดีที่สุดในการแจ้งให้คอมไพเลอร์ทราบว่าไฟล์มี Externs คุณสามารถรวมไฟล์ดังกล่าว
เป็นไฟล์แหล่งที่มาปกติได้โดยใช้แฟล็กบรรทัดคำสั่ง --js
อย่างไรก็ตาม ยังมีอีกวิธีที่เก่ากว่าในการระบุไฟล์ Externs คุณสามารถใช้
--externs
Flag บรรทัดคำสั่งเพื่อส่งไฟล์
externs อย่างชัดเจน เราไม่แนะนำให้ใช้วิธีนี้
การใช้ Externs
คุณใช้ Extern จากด้านบนได้ดังนี้
/**
* @fileoverview Do some math.
*/
/**
* @param {number} x
* @param {number} y
* @return {number}
*/
export function greatestCommonDivisor(x, y) {
while (y != 0) {
const temp = y;
// `myMath` is a global, it and `myMath.div` are never renamed.
const result = myMath.div(x, y);
// `remainder` is also never renamed on instances of `DivResult`.
y = result.remainder;
x = temp;
}
return x;
}
วัตถุประสงค์ของการส่งออก
การส่งออกเป็นอีกกลไกหนึ่งในการตั้งชื่อสัญลักษณ์ให้สอดคล้องกันหลังการ
คอมไพล์ ซึ่งมีประโยชน์น้อยกว่าตัวแปรภายนอกและมักทำให้เกิดความสับสน สำหรับ
กรณีที่ซับซ้อน การหลีกเลี่ยงการใช้ตัวแปรส่วนกลางเป็นวิธีที่ดีที่สุด
การส่งออกจะขึ้นอยู่กับข้อเท็จจริงที่ว่า Closure Compiler ไม่ได้แก้ไขสตริงลิเทอรัล
การกำหนดออบเจ็กต์ให้กับพร็อพเพอร์ตี้ที่ตั้งชื่อโดยใช้ค่าคงที่ทำให้ออบเจ็กต์พร้อมใช้งานผ่านชื่อพร็อพเพอร์ตี้นั้นแม้หลังจากคอมไพล์แล้วก็ตาม
ด้านล่างนี้เป็นตัวอย่างง่ายๆ
/**
* @fileoverview Do some math.
*/
// Note that the concept of module exports is totally unrelated.
/** @return {number} */
export function myFunction() {
return 5;
}
// This assignment ensures `myFunctionAlias` will be a global alias exposing `myFunction`,
// even after compilation.
window['myFunctionAlias'] = myFunction;
หากคุณใช้ Closure Library คุณยังประกาศการส่งออกได้โดยใช้ฟังก์ชัน
goog.exportSymbol
และ goog.exportProperty
ดูข้อมูลเพิ่มเติมได้ในเอกสารประกอบของ Closure Library เกี่ยวกับ
ฟังก์ชันเหล่านี้ อย่างไรก็ตาม โปรดทราบว่าองค์ประกอบเหล่านี้มีการรองรับคอมไพเลอร์พิเศษ
และจะได้รับการแปลงอย่างสมบูรณ์ในเอาต์พุตที่คอมไพล์แล้ว
ปัญหาเกี่ยวกับการส่งออก
การส่งออกแตกต่างจาก Extern ตรงที่การส่งออกจะสร้างเฉพาะนามแฝงที่เปิดเผยเพื่อให้ผู้ใช้ทั่วไปอ้างอิง ภายในโค้ดที่คอมไพล์แล้ว ระบบจะยังคงเปลี่ยนชื่อสัญลักษณ์ exported
ด้วยเหตุนี้ สัญลักษณ์ที่ส่งออกจึงต้องเป็นค่าคงที่
เนื่องจากการกำหนดค่าใหม่ในโค้ดจะทำให้นามแฝงที่เปิดเผย
ชี้ไปยังสิ่งที่ไม่ถูกต้อง
ความซับซ้อนในการเปลี่ยนชื่อนี้จะยิ่งซับซ้อนมากขึ้นเมื่อเกี่ยวข้องกับพร็อพเพอร์ตี้อินสแตนซ์ที่ส่งออก
ในทางทฤษฎี การส่งออกช่วยให้มีขนาดโค้ดเล็กลงได้เมื่อเทียบกับ Extern เนื่องจากยังเปลี่ยนชื่อที่ยาวให้สั้นลงได้ภายในโค้ด ในทางปฏิบัติ
การปรับปรุงเหล่านี้มักเป็นการปรับปรุงเล็กๆ น้อยๆ และไม่คุ้มค่ากับความสับสนที่การส่งออกสร้างขึ้น
นอกจากนี้ การส่งออกยังไม่มี API ให้ผู้บริโภคทำตามในลักษณะเดียวกับที่ Externs ทำ
เมื่อเทียบกับการส่งออก Externs จะบันทึกสัญลักษณ์ที่คุณต้องการแสดง
ประเภทของสัญลักษณ์ และให้พื้นที่แก่คุณเพื่อเพิ่มข้อมูลการใช้งาน นอกจากนี้
หากผู้บริโภคใช้ Closure Compiler ด้วย ก็จะต้องมีไฟล์ภายนอกเพื่อ
คอมไพล์
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[null,null,["อัปเดตล่าสุด 2025-07-26 UTC"],[[["\u003cp\u003eExterns are declarations that inform Closure Compiler about external symbols (like those from native code or third-party libraries) that should not be renamed during compilation.\u003c/p\u003e\n"],["\u003cp\u003eThey act as an API contract, defining what symbols are provided and ensuring type safety by including type annotations.\u003c/p\u003e\n"],["\u003cp\u003eExterns are primarily defined in separate files using the \u003ccode\u003e@externs\u003c/code\u003e annotation, similar to header files in other languages.\u003c/p\u003e\n"],["\u003cp\u003eWhile exports offer an alternative for exposing symbols, they are less versatile than externs and can introduce complexities, making externs generally preferred.\u003c/p\u003e\n"],["\u003cp\u003eExports create aliases for symbols, while externs provide comprehensive API documentation and type information for external code interaction.\u003c/p\u003e\n"]]],[],null,["# Externs and Exports\n\nPurpose of Externs\n------------------\n\n\nExterns are declarations that tell Closure Compiler the names of\nsymbols that should not be renamed during advanced compilation.\nThey are called externs because these symbols are most often defined by\ncode outside the compilation, such a native code, or third-party\nlibraries. For this reason, externs often also have type annotations,\nso that Closure Compiler can typecheck your use of those symbols.\n\n\nIn general, it is best to think of externs as an API contract between\nthe implementor and the consumers of some piece of compiled code. The\nexterns define what the implementor promises to supply, and what the\nconsumers can depend on using. Both sides need a copy of the contract.\n\nExterns are similar to header files in other languages. \n\nExterns Syntax\n--------------\n\n\nExterns are files that look very much like normal JavaScript annotated\nfor Closure Compiler. The main difference is that their contents are never printed\nas part of the compiled output, so none of the values are meaningful,\nonly the names and types.\n\nBelow is an example of an externs file for a simple library. \n\n```gdscript\n// The `@externs` annotation is the best way to indicate a file contains externs.\n\n/**\n * @fileoverview Public API of my_math.js.\n * @externs\n */\n\n// Externs often declare global namespaces.\n\nconst myMath = {};\n\n// Externs can declare functions, most importantly their names.\n\n/**\n * @param {number} x\n * @param {number} y\n * @return {!myMath.DivResult}\n */\nmyMath.div = function(x, y) {}; // Note the empty body.\n\n// Externs can contain type declarations, such as classes and interfaces.\n\n/** The result of an integer division. */\nmyMath.DivResult = class {\n\n // Constructors are special; member fields can be declared in their bodies.\n\n constructor() {\n /** @type {number} */\n this.quotient;\n /** @type {number} */\n this.remainder;\n }\n\n // Methods can be declared as usual; their bodies are meaningless though.\n\n /** @return {!Array\u003cnumber\u003e} */\n toPair() {}\n\n};\n\n// Fields and methods can also be declared using prototype notation.\n\n/**\n * @override\n * @param {number=} radix\n */\nmyMath.DivResult.prototype.toString = function(radix) {};\n \n``` \n\n### The `--externs` Flag\n\n\nGenerally, the `@externs` annotation is the best way to inform\nthe compiler that a file contains externs. Such files can be included\nas normal source files using the `--js` command-line flag,\n\n\nHowever, there is another, older way, to specify externs files. The\n`--externs` command-line flag can be used to pass externs\nfiles explicitly. This method is not recommended. \n\nUsing Externs\n-------------\n\nThe externs from above can be consumed as follows. \n\n```mysql\n/**\n * @fileoverview Do some math.\n */\n\n/**\n * @param {number} x\n * @param {number} y\n * @return {number}\n */\nexport function greatestCommonDivisor(x, y) {\n while (y != 0) {\n const temp = y;\n // `myMath` is a global, it and `myMath.div` are never renamed.\n const result = myMath.div(x, y);\n // `remainder` is also never renamed on instances of `DivResult`.\n y = result.remainder;\n x = temp;\n }\n return x;\n}\n \n``` \n\nPurpose of Exports\n------------------\n\n\nExports are another mechanism for giving symbols consistent names after\ncompilation. They are less useful than externs and often confusing. For\nall but simple cases they are best avoided.\n\n\nExports rely on the fact that Closure Compiler doesn't modify string literals.\nBy assigning an object to a property named using a literal, the object will\nbe available through that property name even after compilation.\n\n\nBelow is a simple example. \n\n```mysql\n/**\n * @fileoverview Do some math.\n */\n\n// Note that the concept of module exports is totally unrelated.\n\n/** @return {number} */\nexport function myFunction() {\n return 5;\n}\n\n// This assignment ensures `myFunctionAlias` will be a global alias exposing `myFunction`,\n// even after compilation.\n\nwindow['myFunctionAlias'] = myFunction;\n \n``` \n\nIf you are using Closure Library, exports can also be declared using the\n`goog.exportSymbol` and `goog.exportProperty` functions.\n\n\nMore information is available in the Closure Library documentation of\nthese functions. However, be aware they have special compiler support\nand will be totally transformed in the compiled output. \n\nIssues with Exports\n-------------------\n\n\nExports are different from externs in that they only create an exposed\n*alias* for consumers to reference. Within the compiled code, the exported\nsymbol will still be renamed. For this reason, exported symbols must be\nconstant, since reassigning them in your code would cause the exposed\nalias to point to the wrong thing.\n\n\nThis subtlety in renaming is especially complicated with respect to exported\ninstance properties.\n\n\nIn theory, exports can allow smaller code-size compared to externs, since long\nnames can still be changed to shorter ones within your code. In practice,\nthese improvements are often very minor, and don't justify the confusion exports create.\n\n\nExports also don't provide an API for consumers to follow in the way externs do.\nCompared to exports, externs document the symbols you intend to expose,\ntheir types, and give you a place to add usage information. Additionally,\nif your consumers are also using Closure Compiler, they will need externs to\ncompile against."]]