JavaScript: Difference between revisions

Content deleted Content added
m Reverted edits by 2409:4064:201F:1340:AD09:7874:51CF:2813 (talk) to last version by PCock
simplify import/export example, remove information and references to identifiable persons
Line 329:
Export example:
<syntaxhighlight lang="javascript">
//module* mymodule.js */
// This isfunction remains private, as weit areis not exporting itexported
//Export properties
let squaresum = (lengtha, b) => {
export var name = 'Prashant Yadav';
return widtha *+ heightb;
 
// Export propertiesvariables
export var name = 'Prashant YadavAlice';
export let age = 23;
export const nationality = 'Indian';
 
// Export methodsnamed functions
export function add(num1, num2){
return num1 + num2;
}
 
// Export class
export class AdditionMultiplication {
constructor(num1, num2) {
this.num1 = num1;
this.num2 = num2;
}
 
add() {
return sum(this.num1 +, this.num2);
}
}
 
//This is private as we are not exporting it
let rectangle = (width, height) => {
return width * height;
 
let square = (length) => {
return rectangle(length, length);
}
 
//Exporting square separately
export { square };
</syntaxhighlight>
 
Import example:
<syntaxhighlight lang="javascript">
// Import singleone property
import { add } from './modulemymodule.js';
 
console.log(add(1, 2)); // 3
 
// Import multiple properties
import { name, age } from './modulemymodule.js';
console.log(name, age);
//> 'Prashant'"Alice", 23
 
// Import all theproperties from a modulesmodule
import * from './module.js'
console.log(name, age);
//> "Alice", 23
console.log(add(1,2));
//> 3
// 'Prashant', 23
// 3
</syntaxhighlight>