JavaScript Tours-Second stop: objects and arrays

Source: Internet
Author: User

Original Source:A first-line Code farm BlogWelcome to share original toBó Lè Headlines

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 string and value types, such as.

12345 vardelivery = {      no: 1,      sendtime: "2014-11-25",      way: "PJS"};

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, as follows.

123456789101112131415 var delivery = {        no: 1,        sendtime: "2014-11-25",        //三个可用配送地址        address: ["北京", "上海", "南京"],        way: "PJS",        //根据逻辑是否支持配送        cansend: function () {            //logic code....            return true;        }    };

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 people may say that there are indexers in C #, attributes, events and so on, in fact, these are the compiler to the syntax of sugar, really not fun, in essence, are some of the methods, some will be fooled by these and 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 "way" 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 strongly typed way value, but there is no metadata in JS, but probably also using a similar method of reflection dynamic binding, said so many , let's take a look at examples.

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:

12 vars = [1, "ctrip"];var a = newArray(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 that we will find a phenomenon, the array incredibly can and objects to pull a piece, the end will make you dazzled, perhaps this is the flexibility of non-type, pros and cons.

We can see that by s.name= "Cnblogs" This class object assignment, you can add the key and value to the array, and you can also output the result value by S.name and s["name", which means that the array is essentially a class, Only in the class to do some high-level package, such as the need to regularly count the length of the size, it can be two ways to output the result value, but this caused the index array and associative array coexistence situation. If in C #, the normal index array, we know that their space in the heap has 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 such a 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

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.