object

Last week, I took the train back to my hometown, and I was with my younger brother who was engaged in Java. What can two program monkeys do together? (πŸ˜€ don’t think much of this article is about the object, but we don’t make object, although it was gay friends, but we don’t gay πŸ˜…, haha), the road is not conscious daily problems, discussion and interaction in the process of communication found that the difference between Java and js data structure, this article will object to the data structure is detailed discussed and summarized. Ok, next please listen to me to a wave of explanation (install force πŸ™ƒ)

Js object

Javascript is a prototype-based language, and objects are the basic data types of javascript. Objects are compound values in the form of key/value, property names are strings, and there are other names for this basic data structure: “Hashtable,” “dictionary,” “Associative array.” Objects are more than just a string to value mapping. In addition to retaining their own properties, they can inherit properties from an object called a stereotype.

Js create object

1. Object direct quantity

var obj = {} // No property object
var obj1 = { name: 'ipenman' } // Attribute name value ipenman
Copy the code

So let’s look at objects that are created by objects directly

As you can see above, a literal creates an Object that contains properties and is a proto, and its prototype is the prototype of Object.

2. Created by the new + constructor

new Object()

var obj = new Object(a)Copy the code







The new function

function Obj(){}
var obj = new Obj()
Copy the code

The stereotype created by new Obj() is composed of the Obj constructor and the Object stereotype.

3.Object.create()

Es5 defines a method called object.create (), which creates a new Object with a mandatory proto as its first argument. Optional. If not specified as undefined, this is the property descriptor and the corresponding property name of the non-enumerable (default) property (that is, the property defined by itself, not the enumerated property on its prototype chain) object to be added to the newly created object. These properties correspond to the second argument of object.defineProperties ().

Eg1: Object. The create (null)

var obj = Object.create(null)
Copy the code

Eg2: Object. The create ({})

var obj = Object.create({})
Copy the code

var obj = Object.create({name:'ipenman'})
Copy the code

Eg3: Object. The create (prototype) * *

var obj = Object.create(Object.prototype)
Copy the code

function Person(){
	this.name = 'ipenman'
}
var obj = Object.create(Person.prototype)
Copy the code

{} is equivalent to new Object() is equivalent to object.create (object.prototype) **

Stereotypes vs. archetypal properties

See github for the prototype and the prototype chain. The article is a long time ago, and will be updated in the next article. Finally: Ask star!

Access to the prototype

var obj = {}
console.log(Object.getPrototypeOf(obj))
Copy the code

Accessing stereotype properties

function obj(){}
obj.prototype.name = 'ipenman'
console.log(Object.prototype) // ipenman
Copy the code

inheritance

This part, will carry out a separate will tell, to update the supplementary link. Click here * *

Access and Settings

access

There are two ways to access js object properties: 1. **2. Square brackets string ([]).

var obj = {name:'ipenman'}
console.log(obj.name) // ipenman
console.log(obj["name"]) // ipenman
Copy the code

Set up the

Setting corresponds to access, dot or []

var obj = {}
obj.name = 'ipenman'
obj["age"] = 24
console.log(obj) // obj { name:"ipenman", age:24 }
Copy the code

台湾国

Js objects vs Java objects

JavaScript: a prototype-based language. Java: An object-oriented language based on OOP classes. In this sense, the difference between JS objects and Java objects is really based on the difference between prototype-based languages and class-based languages.

A comparison of class-based (Java) and prototype-based (JavaScript) object systems

Class-based (Java) Prototype-based (JavaScript)
Classes and instances are different things. All objects are instances.
Define a class through a class definition; Classes are instantiated through constructor methods. A constructor function defines and creates a set of objects.
throughnewOperator to create a single object. The same.
Class definitions define subclasses of existing classes to build a hierarchy of objects. Specify an object as a prototype and build a hierarchy of objects with the constructor
Follows the class chain inheritance properties. Follow the stereotype chain to inherit properties.
Class definition for all instances of a specified classallProperties. Properties cannot be added dynamically at run time. A constructor function or stereotype specifies the initial set of properties. Allows properties to be added or removed dynamically to a single object or to an entire object set.


The resources

Mdn-details of the Object Model The Authoritative Guide to JavaScript

The last

Like and follow! 😁