Let js data types of all methods are common up

Array methods can have a string, the string has an array of methods, while at the same time have a json above two methods, is not it amazing? In fact, I'd like pipe This method is called "data type of deception."

First talk about the two closer js data, called an array, called json. Why acquire both say it? First, Talia is a "father," Yes, all Obj. They tell you not the same place.

json no length, accurate to say that the json length is undefined. In fact, this is just a private property of the object of it, because this is not private property, it is undefined. And there Array.prototype length method, the second array can be extracted with brackets to the corresponding value is:

There are a = [1,2,3];

a[0]//1

a[1]//2

In brackets must put a number, but this feature in fact json be supported. That it gave a chance.

ok, now forge a [1,2,3] object with json.

There are a = {

'0':1

'1':2,

'2':2

} ;

json key value in the key pair can not be explained only as the Number key value, ok being the case, I can use the string of 0,1,2.

Such so to do

a[0] //1

a[1] //2

Seems to be deceived system, of course, is the last step short of length, ok, we directly confer a length.

There are a = {

'0':1

'1':2,

'2':2,

length:3

} ;

One such a fact, when output and [1,2,3] is completely different. To put it plainly, we have used a json, without actually look at the content can be completely fool the human eye.

In the end you can not fool the system eyes? Before the first test we are speaking about the object's method implementation principle. In fact, all of the functions of this method to instantiate objects are pointing to the data that you want to last out this operation in return.

With this reason we try to use this fake json can not use an array of methods. First of all, we should first try to slice.

a.slice () // This method is not being given

[] .Slice.call (a) // 1,2,3 successfully fooled the browser

We try a little depth.

Array.prototype.slice.call (a, 1) // 2,3 indeed taken up.

If this time json object does not have the attributes of length, will return an empty array, the array length instructions in this method will be used. And after our json, and arrays with everything the same, this method after this became, in fact, can fool the browser to perform an array of methods.

Of course, we have another example of a more difficult it slightly, the array has a bunch of their own cycle, such as: find, forEach, findIndex we will test the last of it.

[].findIndex.call(a,(function(e,index){

console.log(e);1,2,3

console.log(index); 0,1,2

}));

No problem at all, in fact, observed after the discovery, the majority array of methods can be used, which means that the method is completely clear json array can also be used, but if you want to use json, but also in line with the key value and length as arrays, but nothing seems to make sense. We can certainly json string methods?

There are a = '123';

with

There are a = {

'0':'1'

'1':'2',

'2':'2',

length:3

} ;

Does not seem in the output when no obvious differences, their a [0] is 1, length also is 3. ok, let's try

'.substring.call(a)//'[object object]'

You can use not being given, that substring recognize this thing. However, the interior has exposed him to the inside of himself that he is a true object of this is to parse out obj. He would eventually return things to come out of this. Of course, you will say, there are new String string if this problem will go into it? Come, let's look at with questions.

var a = new String('123');

''.substring.call(a) //'123';

And after I tested several methods found that although no error, however, the output is the string '[object object]'. People string their target is not at all right, this is actually also reflects the depth of a problem. Although string objects have a reference to an object, or a characteristic of all private property, but the array and object obj and there are some essential differences. Of course comes to an end json, we'll try to play an array of strings.

There are a = [1,2,3];

''.substring.call(a,1)//,2,3;

No error, but the array also brushed back a string of substring method, super cool. But the cutoff value is really quite not what we want. Yes ', 2,3';

In fact, it is not difficult to find an internal array into a '1,2,3', starting with the first interception of the last naturally arises ', 2,3';

In fact, after testing several methods and found an array of strings is still pretty friendly, mostly supported, but then you go into the array, it will be automatically converted to a string with commas. Like [1,2,3] converted into '1,2,3'; but in fact there is no string array method so friendly, but there are ways of making just fine. E.g:

[].slice.call('123',1)//['2','3'];

Into an array of [ '2', '3'] is '123' after automatically go into a [ '1', '2', '3'] taken from the first one, then on start become [ '2', '3'], and it seems good results because save a split ( '') step, but it is only a letter of the alphabet become divided into an array. However, some other method no luck with it, such as:

There are a = '123';

[] .Push.call (a, '4') // error

In fact, most of the test, with a very small number can be found, but there are still a small part can be used.

In short, this thing is actually not in actual combat any practical significance, but to say that if in fact arrays, strings, methods section of json can be generic.

Welcome to click into Leo Lee tour of the front end of the classroom teacher
click to enter the site the most complete full-stack course "Lee Tour Leo - Web front-end, full-stack from zero base to engineer" takes you step in with the fastest time salary of thousands

Guess you like

Origin blog.51cto.com/7669561/2433322