বহিরাগত এবং রপ্তানি
সেভ করা পৃষ্ঠা গুছিয়ে রাখতে 'সংগ্রহ' ব্যবহার করুন
আপনার পছন্দ অনুযায়ী কন্টেন্ট সেভ করুন ও সঠিক বিভাগে রাখুন।
বহিরাগতদের উদ্দেশ্য
Externs হল ঘোষণা যা ক্লোজার কম্পাইলারকে চিহ্নগুলির নাম বলে যেগুলি উন্নত সংকলনের সময় পুনরায় নামকরণ করা উচিত নয়। এগুলিকে বহিরাগত বলা হয় কারণ এই চিহ্নগুলি প্রায়শই সংকলনের বাইরের কোড দ্বারা সংজ্ঞায়িত করা হয়, যেমন একটি নেটিভ কোড, বা তৃতীয় পক্ষের লাইব্রেরি। এই কারণে, বহিরাগতগুলিতে প্রায়শই টাইপ টীকাও থাকে, যাতে ক্লোজার কম্পাইলার আপনার সেই চিহ্নগুলির ব্যবহার টাইপ চেক করতে পারে।
সাধারণভাবে, সংকলিত কোডের কিছু অংশ বাস্তবায়নকারী এবং ভোক্তাদের মধ্যে একটি API চুক্তি হিসাবে externs কে ভাবা ভাল। এক্সটার্নগুলি সংজ্ঞায়িত করে যে বাস্তবায়নকারী কী সরবরাহ করার প্রতিশ্রুতি দেয় এবং ভোক্তারা কী ব্যবহার করার উপর নির্ভর করতে পারে। উভয় পক্ষের চুক্তির একটি অনুলিপি প্রয়োজন।
Externs অন্যান্য ভাষার হেডার ফাইলের মতই।
বাহ্যিক সিনট্যাক্স
Externs হল এমন ফাইল যা দেখতে অনেকটা সাধারণ জাভাস্ক্রিপ্টের মতই ক্লোজার কম্পাইলারের জন্য টীকা করা। প্রধান পার্থক্য হল যে তাদের বিষয়বস্তু সংকলিত আউটপুট অংশ হিসাবে মুদ্রিত হয় না, তাই মানগুলির কোনটিই অর্থপূর্ণ নয়, শুধুমাত্র নাম এবং প্রকারগুলি।
নীচে একটি সাধারণ লাইব্রেরির জন্য একটি 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
কমান্ড-লাইন পতাকা externs ফাইলগুলি স্পষ্টভাবে পাস করতে ব্যবহার করা যেতে পারে। এই পদ্ধতি সুপারিশ করা হয় না.
Externs ব্যবহার করে
উপর থেকে externs নিম্নলিখিত হিসাবে গ্রাস করা যেতে পারে.
/**
* @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;
}
রপ্তানির উদ্দেশ্য
রপ্তানি হল সংকলনের পরে প্রতীকগুলির সামঞ্জস্যপূর্ণ নাম দেওয়ার আরেকটি পদ্ধতি। তারা বহিরাগতদের তুলনায় কম দরকারী এবং প্রায়ই বিভ্রান্তিকর। সাধারণ ব্যতীত সব ক্ষেত্রেই এড়িয়ে যাওয়া হয়।
রপ্তানি এই সত্যের উপর নির্ভর করে যে ক্লোজার কম্পাইলার স্ট্রিং লিটারেল পরিবর্তন করে না। একটি আক্ষরিক ব্যবহার করে একটি প্রপার্টি নামক একটি বস্তু বরাদ্দ করে, বস্তুটি সংকলনের পরেও সেই সম্পত্তি নামের মাধ্যমে পাওয়া যাবে।
নীচে একটি সহজ উদাহরণ.
/**
* @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;
আপনি যদি ক্লোজার লাইব্রেরি ব্যবহার করেন, তাহলে goog.exportSymbol
এবং goog.exportProperty
ফাংশন ব্যবহার করেও রপ্তানি ঘোষণা করা যেতে পারে।
এই ফাংশনগুলির ক্লোজার লাইব্রেরি ডকুমেন্টেশনে আরও তথ্য পাওয়া যায়। যাইহোক, সচেতন থাকুন তাদের বিশেষ কম্পাইলার সমর্থন রয়েছে এবং কম্পাইল করা আউটপুটে সম্পূর্ণরূপে রূপান্তরিত হবে।
রপ্তানি সঙ্গে সমস্যা
রপ্তানিগুলি বহিরাগতদের থেকে আলাদা যে তারা শুধুমাত্র ভোক্তাদের রেফারেন্সের জন্য একটি উন্মুক্ত উপনাম তৈরি করে৷ সংকলিত কোডের মধ্যে, রপ্তানি করা প্রতীকটির নাম পরিবর্তন করা হবে। এই কারণে, রপ্তানি করা প্রতীকগুলি অবশ্যই ধ্রুবক হতে হবে, যেহেতু আপনার কোডে সেগুলিকে পুনরায় বরাদ্দ করার ফলে উন্মুক্ত উপনামটি ভুল জিনিসের দিকে নির্দেশ করবে৷
পুনঃনামকরণের এই সূক্ষ্মতা রপ্তানি করা উদাহরণ বৈশিষ্ট্যের ক্ষেত্রে বিশেষত জটিল।
তাত্ত্বিকভাবে, রপ্তানি এক্সটার্নের তুলনায় ছোট কোড-আকারের অনুমতি দিতে পারে, যেহেতু দীর্ঘ নামগুলি এখনও আপনার কোডের মধ্যে ছোট নামগুলিতে পরিবর্তন করা যেতে পারে। অনুশীলনে, এই উন্নতিগুলি প্রায়শই খুব ছোট হয়, এবং রপ্তানি যে বিভ্রান্তি তৈরি করে তা সমর্থন করে না।
রপ্তানিও ভোক্তাদের জন্য বহিরাগতদের মত অনুসরণ করার জন্য একটি API প্রদান করে না। রপ্তানির তুলনায়, বহিরাগতগুলি আপনি যে চিহ্নগুলি প্রকাশ করতে চান, তাদের প্রকারগুলি নথিভুক্ত করে এবং আপনাকে ব্যবহারের তথ্য যোগ করার জন্য একটি জায়গা দেয়৷ অতিরিক্তভাবে, যদি আপনার ভোক্তারাও ক্লোজার কম্পাইলার ব্যবহার করে থাকেন, তাহলে তাদের কম্পাইল করার জন্য এক্সটার্নের প্রয়োজন হবে।
অন্য কিছু উল্লেখ না করা থাকলে, এই পৃষ্ঠার কন্টেন্ট Creative Commons Attribution 4.0 License-এর অধীনে এবং কোডের নমুনাগুলি Apache 2.0 License-এর অধীনে লাইসেন্স প্রাপ্ত। আরও জানতে, Google Developers সাইট নীতি দেখুন। Java হল Oracle এবং/অথবা তার অ্যাফিলিয়েট সংস্থার রেজিস্টার্ড ট্রেডমার্ক।
2025-07-24 UTC-তে শেষবার আপডেট করা হয়েছে।
[[["সহজে বোঝা যায়","easyToUnderstand","thumb-up"],["আমার সমস্যার সমাধান হয়েছে","solvedMyProblem","thumb-up"],["অন্যান্য","otherUp","thumb-up"]],[["এতে আমার প্রয়োজনীয় তথ্য নেই","missingTheInformationINeed","thumb-down"],["খুব জটিল / অনেক ধাপ","tooComplicatedTooManySteps","thumb-down"],["পুরনো","outOfDate","thumb-down"],["অনুবাদ সংক্রান্ত সমস্যা","translationIssue","thumb-down"],["নমুনা / কোড সংক্রান্ত সমস্যা","samplesCodeIssue","thumb-down"],["অন্যান্য","otherDown","thumb-down"]],["2025-07-24 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."]]