JavaScript Stuff: Objects

Source: Internet
Author: User

1. What is the first object?

Are the "objects" that single kids ' shoes are looking for? Yes, he/she is a living object.

JavaScript is an object-based language, and everything you encounter is almost always an object.

but it's different from class-based languages. then "class" is also God horse, in short, the class is the same kind of things abstract name.

For example, "person" is an abstract name, then you, I, he, is a specific ' person ' object.

Then "abstract" is God horse, abstraction is to extract the essence of things and temporarily do not consider their details.

abstraction is the most powerful tool of thought used by humans in understanding complex phenomena. The purpose of abstraction is to be better specific!

2. What is the second object?

Objects typically have properties and methods.

property represents the static characteristics of an object. method represents the dynamic behavior of an object.

For example, people have "name", "age" and other attributes, there are "eating", "walking" and other methods.

Properties and methods are collectively referred to as the "members" of an object, and are collectively referred to as "attributes" in JavaScript.

That is, the "method" can be regarded as a special "attribute".

ECMA-262 defines an object as: "A collection of unordered attributes whose properties can contain basic values, objects, or functions."

We can think of objects in JavaScript as a set of key-value pairs where "key" is the name of the property and "value" is the value of the property.

In addition, the object has a life cycle, just as people have to experience sickness and death.

Here's a simple JS object:

var person = {  ' cnblog ',  +,  function() {},   function() {}};
3. What does the final object do?

The premise of what the object does is, of course, what the object has, and the object can only do what it is capable of.

Usually a simple thing, an object can be done by itself.

When you need to deal with complex things, you need to collaborate with multiple objects to complete.

In order to collaborate, it is necessary to deal with how the objects communicate with each other.

What is the relationship between objects, how do relationships occur, and so on.

Of course, these problems, each programming language designers have given the corresponding solution.

Our task is to learn to use them flexibly and to avoid unnecessary stepping on the pits.

4. JS Create object

JS creates objects in the following ways:

(1). Use object literals:

var person = {    ' cnblog ',    +}

(2). Use object:

var New  = ' Cnblog '= 100;

(3). Use the function constructor:

function Person (name, score) {  this. Name = name;    this. score = Score}varnew person (' cnblog ', 100);

In fact, creating an object using the object type is essentially created using a function constructor, because object is also a function constructor:

typeof Object    //  "function"

(4). Use Object.create:

var person = Object.create (object.prototype, {  name: {writable:true, configurable: true, Value: "Cnblog" },  score: {writable:true, configurable:true, value:100  }});

Object.create () is a new API for ES5 that creates an object that has a specified prototype and several specified properties.

5. JS built-in object

JavaScript built-in objects are objects that are provided independently of the ECMAScript implementation of the hosting environment.

Common built-in objects are: Object, Function, Array, String, Boolean, number, Date, REGEXP, Error, Math.

JavaScript Stuff: Objects

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.