The target

  • Be able to say why you need objects
  • Ability to create objects using literals
  • The ability to create objects using constructors
  • Be able to tell the execution of new
  • Being able to walk through objects

directory

  • object
  • There are three ways to create objects
  • The new keyword
  • Iterate over object properties

Object 1.

1.1 What is an object

In real life: everything is an object, and an object is a concrete thing, something tangible. For example, a book, a car, a person can be an “object,” as can a database, a web page, a connection to a remote server.

In JavaScript, an object is an unordered collection of related properties and methods, and everything is an object, such as strings, numbers, arrays, functions, and so on.

Objects are made up of properties and methods.

  • Attribute: a characteristic of something, represented by an attribute in an object (common noun)
  • 2. The act of something expressed by means in an object (often a verb).

1.2 Why objects

Variables can be used when storing a single value, and arrays can be used when storing multiple values (groups of values). What if you want to keep a person’s full information?

Object expression structure in JS is clearer and more powerful.

2. Three ways to create an object

There are currently three ways to create objects in JavaScript:

  • Create objects using literals
  • Create an Object using new Object
  • Use constructors to create objects

2.1 Create objects using literals

Object literals: curly braces {} contain properties and methods that express the object.

{} takes the form of key-value pairs

  • Key: equivalent to the property name
  • Value: Equivalent to attribute value, can be any type of value (numeric, string, Boolean, function type, etc.)
Var obj = {uname:' I ', age:'18', sex:'girl', sayhi:function (){console.log(hi); }}Copy the code

Using the object

  • The property inside the object calls: object. Property name, that little dot. It means “of.”
  • Object [‘ attribute name ‘]. Note that attributes in square brackets must be quoted, as we will use later
  • Method calls inside objects: objects. Method name (), which must be followed by parentheses
        obj.sayhi();
Copy the code

Differences between variables, attributes, functions, and methods

  • Variable: Separate declaration assignment, separate existence
  • Properties: Variables in an object are called properties and are used to describe the characteristics of the object without declaration
  • Function: a separate function that can be called by the function name ()
  • Methods: The functions inside an object are called methods. Methods do not need to be declared. The method name (“) is used to describe the behavior and functionality of the object.

2.2 Creating an Object using new Object

var obj = new object ();
Copy the code
  • Add the attributes and methods of the object using the equals = assignment method
  • Each property and method ends with a semicolon

For example,

var mingren = new Object(); // Create an empty object // assign mingren.name = 'Naruto '; mingren.age = '19'; Mingren. Sex = 'male '; Mingren. skill = function () {console.log(' mingren.skill '); } mingren.skill();Copy the code

2.3 Use constructors to create objects

Constructor: is a special function used mainly to initialize an object, that is, to assign initial values to object member variables. It is always used with the new operator. We can extract some common properties and methods from objects and encapsulate them in this function.

It’s abstracting the same code from an object and encapsulating it into a function

Function constructor name (){this. Attribute = value; This. Method = funciton(); } new constructor name ();Copy the code

  1. Use a capital letter to create a function
  2. Our constructor does not need a return to return the result
  3. The constructor must be called using new
  4. Attribute methods must be preceded by this

2.4 Constructors and objects

  • Constructors, such as Stars(), abstract the common part of an object and encapsulate it inside a function. They refer to a general class.
  • Creating an object, such as new Stars, refers to a particular object. The process of creating an object using the new keyword is also called object instantiation

3. Process of executing the new keyword

  1. The new constructor creates an empty object in the inner layer
  2. This will point to the newly created object
  3. Execute the code inside the constructor to add properties and methods to the empty object
  4. Return this object (so there is no return in the constructor)

4. Iterate over the object

for… The IN statement is used to loop over the properties of an array or object.

It’s usually k or key

For (variable in object) for (var k in obj) {console.log(k); // Console. log(obj[k])// output values}Copy the code

5. Summary

  1. Objects make code structure clearer
  2. Object Complex data type object.
  3. Essence: An object is an unordered collection of related properties and methods.
  4. Constructors refer to a general class, such as apples, whether red or green, called apples.
  5. An object instance refers to a particular thing, such as this apple or this handsome guy.
  6. for… The IN statement is used to loop over the properties of an object.