Play to JavaScript oop[0]--base type

Source: Internet
Author: User

Objective

Long ago, it was common to think that JavaScript was doing some web effects and dealing with events.
There are some old stubborn ones around me. NET programmers are still stuck in this perception, and they feel that no backend development is sure to build the system.

The combination of programming language and technology is like a man marrying several chicks.
In the old age,. NET is a grand, JavaScript is side room.
The house is "the Lord of the Harem", not only to keep the family, but also to give you a baby, Eva will inherit the family.
Side room is not so fortunate, in the "harem" there is no status, although food and clothing without worry, but can not manage family affairs, the birth of the baby can only be regarded as concubines, did not inherit the right to business.

But, the time changes, the family business is bigger, the family also branches scattered leaves, the men have become the mouth of the "master."
Career so stable, master too boring, lust is human instinct, master to find some "sexual interest" to meet their own.
The big house for the family Labor, years in her face left a lot of traces, Master has gradually lost interest in her;
Side room leisurely, family affairs without her worry, daily affairs is to dress up themselves, to maintain their youthful luster.
Master this pair of lustful eyes and stared at the side room, so side room finally superior, the upper is of course the status of Ascension, not only can meddle in the family's career, but also let their own EVA management part of the business.
(PS: This example has an inappropriate place, JavaScript appears earlier than. net)

With the popularization and rapid development of the Internet, JavaScript can do more and more things.
What can it do?

    • It can do web-rich client applications, combined with HTML5 and CSS3 it can achieve complex front-end interaction
    • It can be used as a server-side application, such as the current very popular node. JS Framework
    • It can be used as a mobile app, such as an app that you can create iOS and Android platforms via PhoneGap or Titianium.

JavaScript can do a lot of other things, and JavaScript is almost ubiquitous.
Now we can say that, whether it's a PC, a smartphone, or a smart TV, all the devices that provide the JavaScript runtime can do something with JavaScript.

Type overview


The title reveals that this is another series of articles about JavaScript object-oriented programming.
Before formally starting JavaScript object-oriented programming, we need to have a thorough understanding of the type of JavaScript.

A lot. NET programmers can understand the types of data in C #, but do not fully understand the data types of JavaScript.
JavaScript is a weak type of language, although its type is not as diverse as C #, but it is more flexible.

For example: In C #, 1 is the type int, and true is the bool type, and 1 and true are not directly comparable.
In JavaScript, numbers are not just numbers, they can sometimes be used as logical expressions for conditional judgments.

var count = 1;if (count) {    console.log ("hello!");}

In C #, the compiler does not allow you to do this, the following code is not compiled at all.

var count = 1;if (count) {    Console.WriteLine ("Hello!");}

Formally because of this flexibility, make some. NET programmers have a lot of confusion when it comes to writing JavaScript code.
Today we begin this series of journeys with this confusion.

Introduction to data types

The types in JavaScript can be divided into two main categories:

    • Base type: number, String, Boolean, undefined, and null.
    • Complex Type: only object one.

(The latest ECMAScript 6 adds a new base type, symbol)

Definition of the underlying type

1. Number: The numeric type, which includes integers, floating-point numbers, for example: 666, 3.14.
2. String: Represents the type of text data, for example: "Hello", "JavaScript".
3. Boolean: Represents a logical entity that has only two values of true and false.
4. Undefined: Indicates that the variable is undefined or unassigned, and it has a unique value of Undefined.
5. NULL: It represents null, no value, and it has only a unique value of NULL.

Number Type

The. NET FCL has several numeric types, including Int16, Int32, Int64, Float, double, and decimal.
Int16, Int32, and Int64 also contain signed and unsigned versions, respectively.

Int16 Shortvalue = 73;int32 Intvalue = 666;int64 Longvalue = 2147483648; Single Floatvalue = 3.14f;double Doublevalue = 999.99;decimal Decimalvalue = 86400.35627m;

In JavaScript there is only one numeric type, the number type, which includes integers and floating-point numbers.
There are 3 special representations of the number types of javascript:

    • Inifiity: Infinity of Positive
    • -inifity: Negative Infinity
    • NaN: Non-numeric (not a number)

The following JS code defines 5 variables, representing each of the 5 cases, using TypeOf to see the type of the variable:

var intvalue = 123;var Floatvalue = 3.14;var Nanvalue = Nan;var Positiveinfinityvalue = Infinity;var negativeInfinityValue =-infinity;
The typeof action is used to get the type of the variable, which returns a string of type

. NET's FCL has two character types, char and string, respectively.
Char is used to represent a 16-bit Unicode character, and string is used to represent an array of characters.
The char type must enclose the character in a pair of single quotes, and the string type requires a pair of double quotation marks around the character.

char character var ch = ' g ';//string string var str1 = "Hello";//String containing double quotes (using escape character) var str2 = "He said: \" It is a good job!\ "";

JavaScript has only one character type, the string type.
Unlike. NET, JavaScript's string type encloses characters in both single and double quotes, which provides a level of convenience.

1-character string var ch = ' g ';//string var str1 = ' Hello world ';//string with double quotation marks (using escape character) var str2 = "He said: \" It is a good job!\ "";// Use single quotes to denote strings, double quotes without escaping var STR3 = ' He said: ' It is a good job! ';

Use typeof to see the types of variables:

Boolean type

Like. NET, a Boolean type is a logical entity that has only true and false two values.

var flag = true;
var deleted = false;

To view variable types with typeof:

The null type. NET also has a null value that represents an object with a null pointer and a null value for the reference type.
Because C # is a strongly typed language, you cannot assign a null value directly to a variable through the var keyword because the compiler cannot infer its implicit type.

In JavaScript, a null value is a unique value of the null type, conceptually and. NET is consistent.

var foo = null;

To view the type of a variable using typeof


Why typeof null for "object"?

A bug that is actually ECMAScript when typeof null is "Object", which is the case from the beginning of JavaScript, typeof Null should have been "null".

Undefined type

. NET does not have a undefined type, undefined is a JavaScript-specific type that represents a variable that is not assigned, or a variable that is not defined.

Define variables, unassigned var undefinedvalue1;//define variable assignment to undefinedvar undefinedValue2 = undefined;

If you define a variable without assigning a value, the JavaScript engine automatically helps you initialize the variable to the undefined value.

In the chrome console, look at the variable undefinedValue1, whose value is undefined.
Use typeof to see the types of variables:

Determine if a variable has a value

If the variable is already defined, but does not know if the variable has a value, it can be === undefined judged by

var unassigndvar;if (Unassigndvar = = = undefined) {//Logical processing}else{//logic processing}
Determine if a variable exists

The following variable undeclaredvar is undefined, it cannot be used === undefined to judge, otherwise the browser will prompt an error:
Uncaught Referenceerror:undeclaredvar is not defined

You need to use TypeOf to determine if a variable exists.

if (typeof Undeclaredvar = = = ' undefined ') {console.log (' variable undeclaredvar undefined! ‘);} else {console.log (' hello! ');}

JavaScript Programming Tips : To determine whether a variable has a value or to determine whether a variable exists, can be done if (typeof (variable) !== "undefined") this way.

= = and = = = operator

When we introduced the undefined type, we used the = = =, which is the "equality" operator.
In JavaScript, there are two "equality" operators, = = and = = =.

    • = = operator : Used to compare the values of two variables, it ignores the type of the variable. Even though the type of two variables is inconsistent, the JavaScript engine first converts two variables to the same type. the = = operator Returns true as long as the value is the same.
    • = = = Operator : Not only compares the values of two variables, it also compares the types of two variables and returns true only if the type and value are the same.

We compare the numbers 1 and the string "1" with these two operators in the console, and JavaScript thinks that the values of 1 and "1" are equal, but the types are not equal.

"Equal" would have "unequal", JavaScript also provides two "unequal" comparators,! = is a loose comparison symbol,!== is strictly a comparison.

Note: Nan is a special existence, and it is not equal to any value, even if it is itself.

Nan = = nan; False

PS: I did not introduce other comparators in this article, such as >=, <=, &&, | | And so on, because they are consistent with the corresponding operators in C #.

Comparison between undefined and null 1

Undefined and null are two interesting values, and they are often compared.

Look at the code first:

var X;var y = null;console.log (x = = y);

Is it a bit surprising that the variables x and y are of different types and the output in the Chrome console is true?

We can think of it this way: null and undefined, although different types, hold the same value.

Enter the following two lines of code in the Chrome console:

Null = = Undefined;null = = = undefined;

The results are as follows:

Difference 2

Since the values of null and undefined are the same, what difference do they have in arithmetic?

When it comes to operations, the most common of course is the subtraction budget for numbers, and enter the following two lines of code in the Chrome console:

1 + null;1 + undefined;

1 + NULL to get the undefined is Nan.

In JavaScript, there are two conventions when undefined and null participate in an operation:

    • An operation expression contains undefined, and the final result is Nan.
    • An expression that contains null in the operation is treated as 0 at run time.

Does this mean that the values of NULL and 0 are equal? No, null and 0 are still not equal.

Subtraction operations for NULL and numeric

Note: The result of 5/null is infinity,infinity, which means that 5/0 of the results are infinity.

Subtraction operations for undefined and numbers

Undefined and Null comparison summary
    1. Undefined and null hold the same value, undefined = = NULL Returns True
    2. Undefined and null are different types, undefined = = = NULL returns FALSE
    3. Undefined and numbers perform subtraction operations, and the result is always Nan
    4. Null and numeric subtraction operations, NULL is treated as
Comparison of number, Boolean, and string

We have compared the two special existence of null and undefined, since the = = operator can compare the value, then the value of number, Boolean and string, what will be the performance when comparing together?

Comparison of number and Boolean

Conclusion: When you compare number and Boolean types with the = = operator, only the digit 1 is equal to true.

Comparison of number and string

Interestingly, the result of 65 = = "0065" and 3.14 = = "3.14000" is true.

Conclusion: When comparing number and string types, strings are always converted to the corresponding numbers first, and then compared, as long as the values are the same result is true.

Comparison of Boolean and string

"0001" = = True, "1.0000" = = True and "0" = = False result is true.

Conclusion: Compare the Boolean and string types, and the result is false if the string cannot be converted to a numeric type. If it is possible to convert to a numeric type, the string is always first converted to the corresponding number and then compared, and only 1 is equal to true.

Basic Type Summary

So we've covered the basic types of JavaScript, and we're going to use a piece of code and three graphs to summarize the content.

var intvalue = 666;var Floatvalue = 3.14;var strvalue = "Hello"; var boolvalue = True;var Nullvalue = Null;var unassignedVa Lue

Variable type

Comparison of undefined and null

= = Comparison of number, String, and Boolean

Null and undefined are used for arithmetic respectively

References and references

1. "Object-oriented JavaScript 2nd Edition"

2. Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Data_structures

"Attention" Keepfool

Play to JavaScript oop[0]--base type

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.