Cancer in the JavaScript language (bottom)

Source: Internet
Author: User

JavaScript cancer in the language ( bottom )

Pseudo-Array

Js There is no real-world array; This makes the use of arrays very easy, you don't have to set them up, and you never have a cross-border error; But its performance is worse than the real array ;

Typeof operators do not recognize arrays and objects, and to determine whether a value is an array, you also need to check its Construtor Properties: ( You can also use instanceof)

function Isarr (my_value)

{

if (my_value&& typeof my_value = = = ' object ' && my_value.constructor ===array)

{

Console.log (my_value+ "is Aarray");

} else {

Console.log (my_value+ "is not Aarray");

}

}

above the instanceof, Construtor detects arrays created for different frames or windows will give false; when an array is likely to be created in another global execution environment, or an array is passed from one frame to another, the arrays that are created in the array and the second frame have different constructors and are therefore inaccurate.

Here are two of the more reliable array detection methods available:

function Isrealarr (my_value)

{

if (Object.prototype.toString.call (my_value) = = = ' [Object Array] ')

{

Console.log (my_value+ "is Aarray");

} else {

Console.log (my_value+ "is not Aarray");

}

}

another Kind ECMAScript has added Array.isarray (), The purpose of this method is to finally determine whether a value is an array or not, and two do not consider it in that global scope; Support Array.isarray () the browser has Ie9+,ff4+,chrome;


False value

JavaScript in a 6 values are " Fake " , these six values are

False

Null

Undefined

0

"( empty string )

NaN

this inside false itself is a Boolean type, other 5 the other is not.

except this6outside, the others are"really", including objects, arrays, regular, functions, and so on. Note' 0 ',' null ',' false ',{},[]It's all truth .  .

Although these six values are " Fake " , they're not all equal.

Console.log (false = = null)//false

Console.log (false = = undefined)//False

Console.log (false = = 0)//True

Console.log (false = = ")//true

Console.log (false = = NaN)//False

Console.log (null = = undefined)//True

Console.log (Null = = 0)//False

Console.log (Null = = ")//false

Console.log (Null = = NaN)//False

Console.log (undefined ==0)//False

Console.log (undefined== ")//False

Console.log (undefined = = NaN)//false

Console.log (0== ")//True

Console.log (0 = = NaN)//False

for "= =" , the following conclusions are drawn

false true 0 ' also

null undefined compare to true undefined null compare to true

0 in addition to and false Compare to true , there is an empty string "'

empty string "' out of the and false Compare to true , there is also a number 0

of course, for all of this, looking under the spec is the most practical, ES in the ToBoolean explains all the situations

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/58/74/wKioL1SxVOPD6hJHAAGFZqQnGfs250.jpg "title=" image 5. JPG "alt=" wkiol1sxvopd6hjhaagfzqqngfs250.jpg "/>

Object

Js objects are never really empty objects, because they can get member properties from the prototype chain; join you are writing a program to calculate the number of occurrences of each word in a text; we can use the toLowerCase method to convert the text to lowercase format, and then use the the split method passes a regular expression as a parameter to produce an array of words, and then you can traverse the group of words and count the occurrences of the words we see: The following:

Functioncounttime () {

var i;

var word;

var text =

"This Oracle of Comfort Hasso pleased me," +

"That's when I am Heaven ishall Desire" +

"To-see-this-child does," +

"and praise Myconstructor.";

var words =text.tolowercase (). Split (/[\s,.] +/);

var count = {};

for (i = 0; I <words.length; i + = 1) {

Word = words[i];

if (Count[word]) {

Count[word] + = 1;

} else {

Count[word] = 1;

}

}

return count

}

from the results, we found that "this " is 2, but constructoris an unexpected result:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/58/77/wKiom1SxVEPRNDcrAAD8gJfHNno995.jpg "title=" Image 4. JPG "alt=" wkiom1sxveprndcraad8gjfhnno995.jpg "/>

Of course, we can take the test member type to make the correction, as follows:

FunctioncountTime2 () {

var i;

var word;

var text =

"This Oracle of Comfort Hasso pleased me," +

"That's when I am Heaven ishall Desire" +

"To-see-this-child does," +

"and praise Myconstructor.";

var words =text.tolowercase (). Split (/[\s,.] +/);

var count = {};

for (i = 0; I <words.length; i + = 1) {

Word = words[i];

if (typeof count[word] = = = ' number ') {

Count[word] + = 1;

} else {

Count[word] = 1;

}

}

return count

}

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/58/74/wKioL1SxVSvyZdjmAADTo7IifVA241.jpg "title=" Image 3. JPG "alt=" wkiol1sxvsvyzdjmaadto7iifva241.jpg "/>

Well, about JS "cancer" on the introduction to here, follow-up will continue to supplement and perfect, I hope you have a lot of support.


This article is from the "7439523" blog, please be sure to keep this source http://7449523.blog.51cto.com/7439523/1601726

Cancer in the JavaScript language (bottom)

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.