Creating and modifying properties
Create Object
const myObject = {}; //字面量表示法 const myObject = new Object(); //Object() 构造函数
---- modify the properties of the data object is variable
Add property
const printer = {}; //添加属性 printer.on = true; printer.mode = "black and white"; printer['remainingSheetes'] = 168; printer.print = function () { console.log('The printer is printing!'); };
Removing property
delete printer.mode;
Passing parameters
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
Passing objects
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)
Likewise, the object is assigned to a new variable, and then change the copy, the same results with the above-described function parameters.
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
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
Call the object methods
Call the method
const developer = { name: 'Andrew', sayHello: function () { console.log('Hi there!'); } }; developer.sayHello(); developer['sayHello']();
- Array call
-
const myArray = [function alerter() {alter('Hello!'); } ]; //调用alerter()函数 myArray[0]();
Access to their own property
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.
Definition method
--- 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
Definition method
The method also can be defined
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; //这样 }
Note that global variables
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
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.
window object
The window object provided by the browser environment
window as a global variable, represents a running script window exposed to JavaScript code
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
Global variables are properties on the window
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
window.currentlyEating === currentlyEating // true
Global variables and var, let, and const
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.
let eating ='rice'; window.eating === eating //false
The global function is a process window
function learnSomethingNew() { window.open('https://www.udacity.com/'); } window.learnSomethingNew === learnSomethingNew // true
Avoid global variables
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
Name conflict
When two (or more) function depends on the variable with the same name, the name conflicts occur.
- Both functions will try to update or set variables, but these changes will be overwritten each other.
Extract attributes and values
Object.keys()和Object.values()
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']
object in depth
Guess you like
Origin www.cnblogs.com/wydumn/p/11575477.html
Recommended
Ranking