object in depth

  1. Creating and modifying properties

    1. Create Object

      1. const myObject = {};    //字面量表示法
        const myObject = new Object();    //Object() 构造函数
    2. ---- modify the properties of the data object is variable

    3. Add property

      1. const printer = {};
        //添加属性
        printer.on = true;
        printer.mode = "black and white";
        printer['remainingSheetes'] = 168;
        printer.print = function () {
            console.log('The printer is printing!');
        };
    4. Removing property

    5. delete printer.mode;
    6. Passing parameters

      1. Pass primitive types primitive type

        • Primitive type (string, number, Boolean, Null, Undefined) is immutable in. The function parameters (so there is no scope covering) any changes made will not affect the function of the external primitive types, but for the function to create a local copy

        • function changeToEight(n) {
            n = 8; // 无论 n 是什么,它此刻都是 8... 但仅仅是在这个函数中!
          }
          
          let n = 7;
          
          changeToEight(n);
          
          console.log(n);  // 7
      2. Passing objects

        1. The object is variable. If you pass an object to a function, it passes a reference to the object.

          • let originalObject = {
                favoriteColor: 'red'
            };
            
            function setToBlue(object) {
                object.favoriteColor = 'blue';
            }
            
            setToBlue(originalObject);
            
            originalObject.favoriteColor;    //blue
          • JS objects are passed by reference, so if the reference modification, that is, modifying the original object itself (referenced & C language)
        2. Likewise, the object is assigned to a new variable, and then change the copy, the same results with the above-described function parameters.

          1. const iceCreamOriginal = {
                Andrew: 3,
                Richard: 15
            };
            
            const iceCreamCopy = iceCreamOriginal;
            
            console.log(iceCreamCopy.Richard);  //15
            
            iceCreamCopy.Richard = 99;
            
            console.log(iceCreamCopy.Richard);  //99
            
            console.log(iceCreamOriginal.Richard);  //99
    7. Comparing two objects

      • const parrot = {
            group: 'bird',
            feathers: true,
            chirp: function () {
              console.log('Chirp chirp!');
            }
          };
        
          const pigeon = {
            group: 'bird',
            feathers: true,
            chirp: function () {
              console.log('Chirp chirp!');
            }
          };
        
          console.log(parrot === pigeon);   //false
        
          const myBird = parrot;
          console.log(myBird === parrot);   //true
          console.log(myBird === pigeon);   //false
      • Only in the two references to the same object , when compared, will return true
  2. Call the object methods

    1. Call the method

      • const developer = {
          name: 'Andrew',
          sayHello: function () {
            console.log('Hi there!');
          }
        };
        
        developer.sayHello();
        developer['sayHello']();
      • Array call
      1. const myArray = [function alerter() {alter('Hello!'); } ];
        //调用alerter()函数
        myArray[0]();
    2. Access to their own property

      1. this

        • const triangle = {
            type: 'scalene',
            identify: function () {
              console.log(`This is a ${this.type} triangle.`);
            }
          };
          
          triangle.identify();
          // 'This is a scalene triangle.'
        • When the identify () method is invoked, this value will be set to the invoked object.
    3. Definition method

      1. --- define the object constructor

        • function Car(make, model, year) {
              this.make = make;
              this.model = model;
              this.year = year;
          }
          
          var myCar = new Car("Mazda", "Miata", 1990);
        • Then create new new object instance; create a function object type is to declare the type of the name, properties and methods
      2. Definition method

        1. The method also can be defined

          1. function displayCar() {
              var result = `A Beautiful ${this.year} ${this.make} ${this.model}`;
              pretty_print(result);
            }
            
            function Car(make, model, year, owner) {
              this.make = make;
              this.model = model;
              this.year = year;
              this.displayCar = displayCar;  //这样
            }
    4. Note that global variables

      1. How this function calls and call --- determine this value within the function

        • chameleon objects since .lookAround () is invoked as a method, so the value of this .lookAround () in the left portion is located at the point of the call

          1. const chameleon = {
              eyes: 2,
              lookAround: function () {
                 console.log(`I see you with my ${this.eyes} eyes!`);  //用this检索属性
              }
            };
            
            chameleon.lookAround();
        • Global window object

          • const car = {
              numberOfDoors: 4,
              drive: function () {
                 console.log(`Get in one of the ${this.numberOfDoors} doors, and let's go!`);
              }
            };
            
            const letsRoll = car.drive;
            
            letsRoll();
          • When a conventional function is called, this value is the global window object

          • While car.drive is one way, but we still have this function stored in a variable letsRoll in. Since letsRoll () function is called as a regular, so it will point to the inside of this window object.
      2. window object

        1. The window object provided by the browser environment

          1. window as a global variable, represents a running script window exposed to JavaScript code

          2. There are tabs in the browser function, each tab has its own window object, with a window between the tabs of a window object will not be shared

        2. Global variables are properties on the window

          1. The window object is at the highest (global) level, each variable declaration made at the global level will automatically become the property of a window object

          2. window.currentlyEating === currentlyEating // true
        3. Global variables and var, let, and const

          1. Only use var declared variable will add it to the window object, let, const variable declared outside a function, it is not added to the window object as an attribute.

            1. let eating ='rice';
              window.eating === eating    //false
        4. The global function is a process window

          1. function learnSomethingNew() {
              window.open('https://www.udacity.com/');
            }
            
            window.learnSomethingNew === learnSomethingNew  // true
        5. Avoid global variables

          1. Tight coupling

            • Developers are tightly coupled code used to represent too dependent on the details of each other. Changing piece of code inadvertently alter the function of other code
          2. Name conflict

            1. When two (or more) function depends on the variable with the same name, the name conflicts occur.

            2. Both functions will try to update or set variables, but these changes will be overwritten each other.
  3. Extract attributes and values

    1. Object.keys()和Object.values()

      1. const dictionary = {
          car: 'automobile',
          apple: 'healthy snack',
          cat: 'cute furry animal',
          dog: 'best friend'
        };
        
        console.log(Object.keys(dictionary));    // ['car', 'apple', 'cat', 'dog']
        console.log(Object.values(dictionary));    // ['automobile', 'healthy snack', 'cute furry animal', 'best friend']

Guess you like

Origin www.cnblogs.com/wydumn/p/11575477.html