JavaScript-basic syntax (4)

Source: Internet
Author: User

JavaScript-basic syntax (4)
Global method and Number object

Demonstrate global method and Number

Alert (parseInt ("123" + 1); outln (parseInt ("abc"); // NaN, invalid var val = parseInt ("abc "); outln ("value =" + isNaN (val); // isNaN determines if it is illegal var val2 = parseInt ("123 afsa"); // val2 = 123 outln (val2 ); // It should be noted that if the parameter starts with a number, the parseInt method will keep the preceding valid number, and the number will not be converted if it is left behind, therefore, regular expressions are generally used to determine whether each element is a valid number. // convert the specified hexadecimal format to the decimal var val3 = parseInt ("123", 10 ); // 123 var val4 = parseInt ("110", 2); // 6, // convert decimal to other hexadecimal values, the Number object var num = new Number (6); outln (num. toString (2); // 110, specify 6 as binary var num1 = 60; // no new Number outln (num1.toString (16) here )); // 3c // note that num1 may be a numerical value, but how can a numeric value use the toString () method? // Js is an object-based Variable regardless of the Code, functions, etc.) are all objects at the underlying layer. // when Number is used, new Number is rarely used, which is similar to automatic packing in Java.

Js special statement for in statement:


Format: for (variable in object) // statement for traversing the object
For (I in arr ){
Document. writeln (I );
}

Js custom object

Because Javascript is based on objects, it does not have the ability to describe things. However, in Js, functions can be used to simulate object-oriented descriptions.

First, add more attributes and actions to the object.
Function Person () {// equivalent to the constructor // alert ("Person constructor run");} // creates an object by describing it, new var p = new Person (); // Add attributes to the object in Js, with p. the property name can be p. name = "a"; p. age = 1; p. show = function () {// defines the p object function alert ("show is running" + this. name + this. age);} p. show ();

Second, it is used for encapsulation. It is more about describing things.

function Person(name,age){            this.name = name;            this.age = age;            this.setName = function(name){                this.name = name;            }            this.getName = function(){                return this.name;            }        }         var p = new Person("a",1);         alert(p.getName());



Third: Use {} to define the key-value pairs of attributes and values

Var P = {name: "haha", "age": 100, // key-value pair. The value must be a colon and the key may not be "getName": function () {// key-value pairs and key-value pairs are separated by commas (,). return this. name ;}, getAge: function () {return this. age ;}}// alert (P. getAge (); // note that the detection is not strict. It is written as lower case p and no error is reported. // There are two methods for object calling members: object. attribute name object ["attribute name"] alert (P ["name"]); alert (P ["name"] + P ["getAge"] (); // note that ()/* function Person (name, age) {this. name = name; this. age = age; this. setName = function (name) {this. name = name;} this. getName = function () {return this. name ;}} var p = new Person ("a", 1); for (I in p) {document. writeln (p [I]);} */var map = {3: "you", 5: "Me", 4: "It"}/* for (I in map) {document. writeln (map [I]);} outln (map ["5"]); // note that if it is a number, "" */var getKey = function (key) cannot be added) {return map [key];} var mm = getKey (5); alert (mm );

Some Js naming formats

Define an object variable, oXX
Int type variable iXX
Boolean bXX
String variable sXX

Some complex definition formats
  var mm = {          // name:["sdf","sd","dfg","cd"],age:[13,11,12,13]           names:[{name:"a"},{name:"b"},{name:"c"}]       }         //alert(mm.name[2]);         //alert(mm.names[1].name);         for(var i = 0;i
 
  

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.