Method Definitions and Namespace

Functions, including generators and async functions, can be assigned to properties within object initializers.


RESETRUNFULL
<!DOCTYPE html><html><body><script>

var o = {
  f1: function() { return 1; },
  f2() { return 2; },
  ['f' + 3]() { return 3; },   // computed method name
  get f4() {return 4;},
  set f5(v){this.value=v;},
   *f6(){},
   async f7(){}};console.log(o.f1(), o.f2(), o.f3(), o.f4); // 1 2 3 4o.f5=5; console.log(o.value);  // 5

</script></body><html>

The syntax is sometimes used to declare a 'namespace' to group related functions and variables.


RESETRUNFULL
<!DOCTYPE html><html><body><script>

var yourNamespace = {
    m1: function() {},m2: function() {},p1:100};yourNamespace.m1();

</script></body><html>