javascript-Basic Grammar (4)

Source: Internet
Author: User

Global method, and Number object

Show global methods and number for the global
  Alert (parseint ("123" + 1));         Outln (parseint ("abc"));//nan, illegal         var val = parseint ("abc");         Outln ("value =" +isnan (val)),//isnan to determine whether the illegal         var val2 = parseint ("123AFSA");//val2 = 123         outln (VAL2);//Note that If the argument starts with a number, the Parseint method retains the previous valid number, followed by discard         //And the number is not converted, so the regular expression is used to determine whether the elements are valid numbers          //Specify the binary format to decimal         var val3 = parseint ("123", "ten"),//123         var val4 = parseint ("2",//6,            //decimal to other binary, need to use Number object         var num = new Number (6);         Outln (num.tostring (2));//110, specify 6 as binary            var num1 = 60;//There is no new number         outln (num1.tostring);//3c         Note there may be a question here, that is, NUM1 is a numeric value, and how the value can be used with the ToString () method         //js is an object-based, regardless of the write divine Code) variables, functions, etc.) at the bottom are objects,         //So when using number, seldom use the new Number, which is similar to automatic boxing in Java

JS's unique statement for in statement:


format, for (variable in object)//statement that iterates over an object
For (i in arr) {
Document.writeln (i);
}

JS Custom Object

Because JS is based on the object, so he does not have the ability to describe things, but in JS can use the function to simulate the object-oriented description

First: Add more properties and behaviors to the object
function person () {//equals constructor           //alert ("person constructor Run");        }         By describing the creation of the object, new         var p = new person ();         JS in the object to add attributes, with the P. Property name can be        p.name = "a";        P.age = 1;        P.show = function () {//define P-Object Functions            alert ("Show is Running" +this.name+this.age);        }        P.show ();

The second type: for encapsulation, more in 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 property and value of the key-value pair way

var P = {name: "haha", "age": 100,//key-value pair, the value must be a colon, the key can be not "getName": function () {//key-value pair and key-value pairs are separated by commas            return this.name;            },getage:function () {return this.age; }}//Alert (P.getage ());//note detection is not strict, written in lowercase P, also do not error//Object Call member There are two ways, object. Property name Object [property name] Alert (        p["name"]);          Alert (p["name"]+p["Getage"] ());//Note The calling method is followed by ()/* 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: "I", 4: "It"}/*for (i in map) {Document.writeln (map         [i]); } outln (map["5"]);//Note If it is a number, do not add "*/var GetKey = function (key) {return map[Key];         } var mm = GetKey (5); alert (mm);

Some naming formats for JS

Define an object variable, Oxx
type int variable IXX
Boolean type BXX
Type 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<mm.names.length;i++) {             alert (mm.names[i].name);         }


javascript-Basic Grammar (4)

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.