Namespace در زبان برنامه نویسی TypeScript
Namespace یا فضای نام روشی برای گروه بندی منطقی کدهای مرتبط به هم است. در زبان JavaScript همه متغیرها به صورت سراسری قابل دسترسی هستند. اگر در یک پروژه چندین فایل جاوا اسکریپت وجود داشته باشد، ممکن است مقدار یک متغیر در یک فایل باز نویسی شود. این موضوع باعث بروز مشکل “global namespace pollution problem” می شود. وجود فضای نام در تایپ اسکریپت از بروز اینگونه مشکلات جلوگیری می کند.
تعریف یک namespace
برای تعریف یک فضای نام از کلمه کلیدی namepsace استفاده می کنیم. مانند نمونه زیر:
1 2 3 4 | namespace SomeNameSpaceName { export interface ISomeInterfaceName { } export class SomeClassName { } } |
برای دسترسی به کلاس ها و اینترفیس هایی که خارج از فضای نام قرار دارند، باید از کلمه کلیدی export استفاده شود. برای دسترسی به کلاس ها و اینترفیس ها موجود در یک فضای نام دیگر باید مانند نمونه زیر عمل کنیم:
1 | SomeNameSpaceName.SomeClassName; |
اگر فضای نام مورد نظر ما در یک فایل جدا قرار داشته باشد، باید مانند نمونه زیر آن را مشخص کنیم:
1 | /// <reference path = "SomeFileName.ts" /> |
مثال زیر نحوه استفاده از فضای نام در زبان تایپ اسکریپت را نشان می دهد:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | FileName :IShape.ts ---------- namespace Drawing { export interface IShape { draw(); } } FileName :Circle.ts ---------- /// <reference path = "IShape.ts" /> namespace Drawing { export class Circle implements IShape { public draw() { console.log("Circle is drawn"); } FileName :Triangle.ts ---------- /// <reference path = "IShape.ts" /> namespace Drawing { export class Triangle implements IShape { public draw() { console.log("Triangle is drawn"); } } FileName : TestShape.ts /// <reference path = "IShape.ts" /> /// <reference path = "Circle.ts" /> /// <reference path = "Triangle.ts" /> function drawAllShapes(shape:Drawing.IShape) { shape.draw(); } drawAllShapes(new Drawing.Circle()); drawAllShapes(new Drawing.Triangle()); } } } |
کد فوق بعد از کامپایل شدن:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | //Generated by typescript 1.8.10 /// <reference path = "IShape.ts" /> var Drawing; (function (Drawing) { var Circle = (function () { function Circle() { } Circle.prototype.draw = function () { console.log("Cirlce is drawn"); }; return Circle; }()); Drawing.Circle = Circle; })(Drawing || (Drawing = {})); /// <reference path = "IShape.ts" /> var Drawing; (function (Drawing) { var Triangle = (function () { function Triangle() { } Triangle.prototype.draw = function () { console.log("Triangle is drawn"); }; return Triangle; }()); Drawing.Triangle = Triangle; })(Drawing || (Drawing = {})); /// <reference path = "IShape.ts" /> /// <reference path = "Circle.ts" /> /// <reference path = "Triangle.ts" /> function drawAllShapes(shape) { shape.draw(); } drawAllShapes(new Drawing.Circle()); drawAllShapes(new Drawing.Triangle()); |
خروجی مثال:
1 2 | Circle is drawn Triangle is drawn |
فضای نام تو در تو
شما می توانید یک namespace را در داخل یک namespace دیگر تعریف کنید. مانند نمونه زیر:
1 2 3 4 5 | namespace namespace_name1 { export namespace namespace_name2 { export class class_name { } } } |
برای دسترسی به فضای نام داخلی باید از عملگر نقطه (.) استفاده کنیم:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | FileName : Invoice.ts namespace tutorialPoint { export namespace invoiceApp { export class Invoice { public calculateDiscount(price: number) { return price * .40; } } } } FileName: InvoiceTest.ts /// <reference path = "Invoice.ts" /> var invoice = new tutorialPoint.invoiceApp.Invoice(); console.log(invoice.calculateDiscount(500)); |
کد فوق بعد از کامپایل شدن:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | //Generated by typescript 1.8.10 var tutorialPoint; (function (tutorialPoint) { var invoiceApp; (function (invoiceApp) { var Invoice = (function () { function Invoice() { } Invoice.prototype.calculateDiscount = function (price) { return price * .40; }; return Invoice; }()); invoiceApp.Invoice = Invoice; })(invoiceApp = tutorialPoint.invoiceApp || (tutorialPoint.invoiceApp = {})); })(tutorialPoint || (tutorialPoint = {})); /// <reference path = "Invoice.ts" /> var invoice = new tutorialPoint.invoiceApp.Invoice(); console.log(invoice.calculateDiscount(500)); |
خروجی مثال:
1 | 200 |
هیچ نظری ثبت نشده است