JavaScript Tours-Second stop: objects and arrays

Source: Internet
Author: User

Original: JavaScript Tour-second stop: objects and arrays

A sleep to noon, originally prepared to wash and continue to sleep, but think of nothing, still have to continue this series, walk through the variable first station, the second stop we look at objects and arrays.

One: Object

Speaking of objects, we do not naturally think of the object-oriented self-encapsulation of a class, the same JS also follow this code, in the Web programming almost every day in the use of JSON. Yes

This is an object, but the fields below this object are both string and value types, such as.

1     var delivery = {2         no:1,3         sendtime: "2014-11-25", 4         By: "PJS"5     };

From the above you can see that delivery only have basic strings and numeric types, of course, you can play more than that, in addition to the basic type, we can also define an array type,

function Type field, just like this.

1     varDelivery = {2No:1,3Sendtime: "2014-11-25",4 5         //three available shipping addresses6Address: ["Beijing", "Shanghai", "Nanjing"],7 8By: "PJS",9 Ten         //whether the distribution is supported by logic OneCansend:function () { A             //logic code .... -             return true; -         } the};

Now our delivery class is rich in an instant, there is a wealth of available delivery address, there is a logical operation can be distributed, is not there a C # class feeling? Some might say,

C # There are indexers, attributes, events and so on, in fact, these are the compiler to give the syntax of sugar, really is not fun, essentially are some methods, some people will be these paste to get

And did not see the essence of things.

Now the object is created, and the next step is how to use it? There are usually two ways of doing this:

<1> "." Operator

This way, I think people should know, so there is nothing to say, such as: Delivery.way.

<2> Index method

This does not know how many people know, is to use a string index method to find, such as I want to find delivery in the string "Way" field value? Because this "I" string

May come from the user's input, then this situation in C # is called late binding, can only use reflection to find the class metadata, and then output the strong type of the value of the ways, but in JS certainly

There is no metadata, but probably also used a similar reflection method of dynamic binding, said so much, we take a look at the example.

Some people may want to ask, JS in the inside is not hash, I can only say unfortunately, not like C # can decompile or look at IL, but in JS you can't see the bottom to do some

What, but I think that looks like a hash, in fact, the same as C # reflection, after all, key is a string, and value is a variety of types have, string,function,int,array and so on,

There are value types, there are reference types, these hodgepodge are put to value, I feel bad operation is a little, also does not conform to the design specification, since feel is not hash, in the internal search "ways"

Time can only be used in the way of traversal.

Two: arrays

To talk about arrays, first look at the definition of arrays, two ways, literal and array constructors, such as:

1         var s = [1, "Ctrip"]; 2         var New Array (1, "Ctrip");

But if you look closely, you may find that the literal can do, the array constructor can do, the literal cannot be done, the array can also do, such as the initialization of an array of

Number. The first parameter of the array constructor.

Because I do not see the underlying code in the array, it is very sad, so I can only guess, the literal [] is just JS to provide us with the syntax of sugar, the underlying should still call array

A second overloaded function.

The second interesting thing is, we will find a phenomenon, the array incredibly can and objects to pull a piece, the end will be confusing you, perhaps this is the type of flexible

And the pros and cons of it.

We can see that by s.name= the "cnblogs" of such objects, you can add key and value to the array, and you can also pass S.name and

s["name"] Output result value, this means that the array is essentially a class, but in the class to do some high-level package , such as the need for timing statistics length size, also caused by

To output a result value in two ways, but this results in the coexistence of an indexed array and an associative array. If in C #, an ordinary indexed array, we know that they are empty in the heap

Have continuity, so you can use the index to achieve O (1) lookup, but in JS so various types mixed together, I think there should be no way to do hash lookup, because this hash

function is really difficult to do, let me write may not write out, but it is also very regrettable that the source of the array is not visible, so there is no say.

Then there is nothing to say about the array, what for and Forin Traversal, array of commonly used methods, such as Pop,push,join,unshift,shift, we can not see the source code,

You can only see the use of the name of righteousness.

JavaScript Tours-Second stop: objects and arrays

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.