Skip to content

Instantly share code, notes, and snippets.

@abhisekp
Last active March 16, 2018 02:55
Show Gist options
  • Save abhisekp/8adc974baa13c42a859d5c1a214ab256 to your computer and use it in GitHub Desktop.
Save abhisekp/8adc974baa13c42a859d5c1a214ab256 to your computer and use it in GitHub Desktop.
Object Property Assignment in JavaScript — http://bit.ly/js-obj

Object Assignments

  1. Using Dot Notation

    const student = {
     name: 'Abhisek'
    };
    
    student.age = 109; // dot operator assigment
  2. Using Bracket Notation

    const student = {
     name: 'Abhisek'
    };
    
    student['age'] = 150; // bracket assignment
  3. Using Object Spread

    let student = {
      name: 'Abhisek'
    };
    
    student = {
     ...student, // object spread
     age: 200
    } //➔ {name: 'Abhisek', age: 300}
  4. Using Object.assign

    const student = {
     name: 'Abhisek'
    };
    
    Object.assign(student, {
     age: 300
    }); //➔ {name: 'Abhisek', age: 300}
  5. Using Object.defineProperty

    const student = {
     name: 'Abhisek'
    };
    
    Object.defineProperty(student, 'age', {
     value: 400,
     writable: true,
     configurable: true,
     enumerable: true
    }); //➔ {name: 'Abhisek', age: 300}
  6. Using Reflect.defineProperty

    const student = {
     name: 'Abhisek'
    };
    
    Reflect.defineProperty(student, 'age', {
     value: 600,
     writable: true,
     configurable: true,
     enumerable: true
    }); //➔ true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment