day03--classes, values, and objects

Source: Internet
Author: User
Tags parse string string methods string to number

1.js Digital-nan and Infinity

1, in JS, there is a special value, called Nan (not a number), indicating that the operation to return a numeric value is not returned, such as 0 divided by 0 operation, in other languages will be reported errors or exceptions, and in JS, 0/0 will return Nan, do not interrupt code execution.

0/0;  //NaN

2. Any operation involving Nan will return Nan, for example

1 + 0/0; //NaN

3. Nan is not equal to any value, including itself, for example

console.log(NaN == NaN);  //false

JS provides a isNaN method to determine if it is Nan, as

isNaN(0/0);  //true

4. Infinity is used to denote positive infinity,-infinity is used to denote negative infinity, for example:

1/0;  //Infinity-1/0; //-Infinity

5, Infinity and Nan

Infinity * 0;  //NaN
1、0/0返回NaN
2、1/0返回Infinity
3、Infinity*0返回NaN
4、NaN参与运算都返回NaN
5、NaN不等于NaN
6、用isNaN判断是否为NaN


二.js类型-字符串

1. In JS, the string can be expressed in single quotation marks (') or double quotation marks (' '), for example

var str1 = ‘hello‘, str2 = "hello";

    • Double quotation marks in single quotation marks

    • Double quotes with double quotation marks

    • Double quotes with double quotation marks

var ' Hello,  "Zhang San"'"Hello, ' Zhang San ' " " Hello, \ "Zhang San \" " ; Console.log (str);

Iv. Direct Volume

1, can be directly used in the program data values, called direct volume.
Such as:

12 //表示是数字1.2 //表示是小数"hello world" //表示字符串‘Hello‘ //表示字符串true/flase //表示布尔值null //空值{a:1,b:2} //表示对象[a,b,c,d,e] //表示数组


V. Use of strings

1. Use the "+" operator to stitch strings together.

var a = "Hello" +"world!";

2. String is a built-in object that provides many common methods that can be called.

var s = "hello javascript";s.charAt(0);//返回第一个字符s.charAt(s.length -1); //返回最后一个字符s.substring(1,4);//取子串s.indexOf("o");//返回该字符出现的位置s.lastIndexOf("a");//返回最后一次字符出现位置s.split(" ");//返回按空格分割成的数组字符s.replace("h","H");//字符替换s.toUpperCase();//字符串转成大写

Note: strings are fixed, and string methods return new strings.

Vi. Global objects for JS

1. When the JS interpreter starts (or when the browser loads a new page), it creates a new global object and gives it some initial properties:

Global properties:

    • Undefined: no value defined
    • Infinity: Infinity of Positive
    • NaN: value is not a numeric value

Global function: '

    • IsNaN (): Determines whether the value is non-numeric.
    • Eval (): Executes a string as a script code
    • Parsefloat (): Parse string and return floating-point number
    • parseint (): Parses a string and returns an integer
    • decodeURI (): Decode URI
    • encodeURI (): Encodes a string
    • String (): Convert to String
    • Number (): Convert to Digital
    • Date (): Convert to date

2. The global object can be referenced with the This keyword.

var g = this;

Console.log (IsNaN (123)); Console.log (parseint ("123")+5); Console.log (Parsefloat ("123.11"));varurl = encodeURI ("http://www.itbegin.com/it Study/"); Console.log (URL);//after the code space, Chinese will transcodeconsole.log (decodeURI (URL)); Console.log (String ("ABC") ; Console.log (Number ("123.11")+456);vars = parseint ("123.11")+456; Console.log (s);var Global= This;//defines a global variable that references a global objectConsole.log (Global. parseint ("123"));

Eight, explicit type conversion

1, JS in a lot of automatic type conversion.

var a = 1;//a的类型的数字a = "abc";//这里a的类型变为字符型

2, explicit conversion, you can make the code more readable.

var a = Number("3");var s = String(false);var b = Boolean([]);

3, JS in some operators will do implicit type conversion.

var a = 1;a+"";//此表达式就会转换为字符串

Console.log (Number ("123"));//string to numberConsole.log (String (false));//Boolean to Stringvarb = Boolean ("false"); Console.log (b);//String Goto boolean valuevarn =Ten; Console.log (n.tostring ());//Numeric to StringConsole.log (N.tostring (2));//numeric to binary stringConsole.log (parseint ("abc112"));//parse the numeric characters as much as possible and ignore the following. Console.log (Parsefloat ("3.55 ABC")); Console.log (Parsefloat (". 1")); Console.log (parseint ("0.1")); Console.log (parseint (". 1")); Console.log (Parsefloat ("$88.12"));

A core JavaScript built-in object, the ECMAScript implementation provides an object that is not dependent on the hosting environment

These objects already exist (instantiated) before the program executes. ECMAScript, called the Global Object, is divided into the following

1, the Value property of the Global object (Value properties of the the global objects). There are nan,infinity,undefined.

2, the global object of the Function property (function Properties of the the global objects). Have eval,parseint,parsefloat,isnan,isfinite,decodeuri,encodeduri,encodeuricomponent

3, the global object of the constructor (Class) property (Constructor properties of the the global objects). Have Object,function,array,string,boolean,number,date,regexp,error,evalerror,rangeerror,referenceerror,syntaxerror , Typeerror,urierror.

4, the global object of other attributes (other properties of the "global Object"), can be seen as a static class in Java, can be used directly with the class name + dot + method name. There are math,json.

The ECMAScript specification mentions that these global objects have a writable attribute, that is, writable is true, and the enumeration (Enumerable) is false, that is, you cannot use the for in enumeration. ECMAScript, there's this one.

Unless otherwise specified, the standard built-in properties of the global object has attributes {[[writable]]: true, [[E Numerable]]: false, [[configurable]]: true}.







An expression

1. The simplest expression is the "original expression", which includes constants or direct quantities, keywords, and variables.

var b = 1.1;var s = "itbegin";

2. The object initialization expression is actually used to create the newly created object.

var obj = {a:1,b:2};

3, the array initialization expression is actually used to create an array of new.

var a = [];var b = [1,2,3];var c = [1,,4];//数组4个元素,其中两个是undefined

4, function definition expression, used to define a new function.

var area = function(r){return 3.14*r*r;}//圆面积函数

5, attribute expression, through "." Symbol to access the properties of the object.

var o = {a:1,b:1};o.a;//表达式o的a属性o.b;//表达式o的b属性

6. The invocation expression is a syntax for calling a function or method.

f();//调用函数f(


day03--classes, values, and objects

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.