EXERCISE-8 MSD LAB PROGRAMS
EXERCISE-8 MSD LAB PROGRAMS
}
}
// Gadget Class object creation
const g1: Gadget = new Gadget(180, 'SmartPhone', 'Mobile');
// invoking getProduct method with the help of Gadget object
g1.getProduct();
// invoking getProductId method with the help of Gadget object
g1.getProductId();
// product name property access with Gadget object
console.log('Product Name : ' + g1.productName);
OUTPUT: 8(a)
Exercise – 8(b) (Typescript – Namespaces)
AIM:
Write a Typescript program to create a namespace called ProductUtility and place the
Product class definition in it. Import the Product class inside productlist file and use it.
Description:
Namespace is basically has collection of classes, interfaces, variables, functions together in one
file. A Namespace is used to group functions, classes, or interfaces under a common name. The
content of namespaces is hidden by default unless they are exported. Use nested namespaces if
required. The function or any construct which is not exported cannot be accessible outside the
namespace. To import the namespace and use it, make use of the triple slash reference tag.
Procedure Steps:
Step 1: Create a file namespace_demo.ts
Step 2: Create another file namespace_import.ts
The file in which the namespace is declared and the file which uses the namespace to be
compiled together. It is preferable to group the output together in a single file. You have
an option to do that by using the
--outFile keyword.
To import the namespace and use it, make use of the triple slash reference tag.
Step 3: Open Node.js command prompt, change directory to the folder in which Namespace file
resides and run the below command from the command line:
D:/MSD LAB/Typescript> tsc --outFile namespace.js namespace_demo.ts
namespace_import.ts
Step 4: Run the namespace.js file from the command line :
D:/MSD LAB/Typescript> node namespace.js
SOURCE CODE: namespace_demo.ts
namespace ProductUtility {
export class Product
{
productId: number;
productName:string;
quantity: number;
price: number;
constructor( productId: number, productName:string, quantity: number, price: number)
{
this.productId =productId;
this.productName =productName;
this.quantity=quantity;
this.price = price;
console.log("Product Name : "+productName);
}
CalculateAmount(){
var totalPrice= this.price * this.quantity;
return totalPrice;
return this.productId, this.quantity, this.price;
}
}
}
namespace_import.ts
// Accessing Namespace
/// <reference path="./namespace_demo.ts" />
let paymentAmount = new ProductUtility.Product(180,"Apple", 2, 60500);
console.log("Product Id :"+paymentAmount.productId);
console.log("Product Quantity : "+paymentAmount.quantity);
console.log("Product Price : "+paymentAmount.price);
console.log("Amount to be paid:"+paymentAmount.CalculateAmount());