JS built-in objects

Source: Internet
Author: User
Tags object object hasownproperty

Standard built-in objects
    • Constructor Object

Object Boolean String number Function Array RegExp Date Error

    • Other objects

Math Json Global Object

1. Object

Methods for instantiating objects

var obj = {' name ': Mary, age:24};

 The object constructor has no instance object properties and methods

Object.creat

--Create a new object based on a prototype object

var proto = {a:1, B:2};var obj = Object.creat (proto);
Object.prototype.toString

--GET method caller standard type

var obj = new Object (); obj.tostring ();  "[Object Object]"
Object.prototype.hasOwnProperty

--Determine whether a property is an object's own property

var obj = {a:1, b:2};
OBJ.C = 3;
Obj.hasownproperty ("C"); True
Obj.hasownproperty ("a"); False
2. String, number, BooleanBoolean

Converting other types to Boolean values

Numbers: 0, Nan->false

String: ""->false

Undefined->false

Null->false

Object->true

String

Use of strings

String.prototype.indexOf

--Gets the index position of the substring in the string ( returns 1 if it does not exist )

String.prototype.replace

Find string replaced with target character

A regular expression is used here.

String.prototype.split

--Split the string into a string array by the delimiter

NumberNumber.prototype.toFixed

--Rounds num to a number that specifies the number of decimal digits

3. Array

Creation of arrays

var true function (){}];
var arr = new Array (1, "a", true, function () {});
    • Constructor object Properties, method

--prototype, IsArray

    • Prototype object properties, methods

--constructor, splice, ForEach, Find, concat, pop, push, reverse, shift, slice ...

    • Instance object properties, methods

--Length

Array.prototype.splice

--Add or remove elements from an array, returning the list of deleted elements

Array.prototype.splice

--Iterate over the array elements and invoke the callback function

The resulting result is: 2 True 5 true 9 true

Skipped an empty element.

4. Function

Instantiating a function method

Function.prototype.apply

--To specify the caller of the function and the function parameters by parameter, and then execute the function

functionPoint (x, y) { This. x =x;  This. y =y;} Point.prototype.move=function(x, y) { This. x + =x;  This. Y + =y;}varp =NewPoint (0, 0);p. Move (2, 2);//move to point 2varCircle = {x:1, y:1, r:1};p. move.apply (Circle, [2, 1]);//{x:3, y:2, r:1}

Function.prototype.bind

--Same as specifying function callers and function arguments, but returning this function should refer to the

Make the following changes based on the above code:

var circlemove = P.move.bind (Circle, 2, 1// Set Timer to move the circle after 1s
Custom Object Builder

Sub-class constructors

Function call mode
    • ()
    • Apply, call
function parameters
    • The number of formal participation arguments is not necessarily equal
    • Value passing
    • To check implementation function overloading by parameter type
Argument

Compares an indefinite number of numeric sizes and returns a large number:

function Max () {       var _max = arguments[0];         for (I =0; i < arguments.length; i++) {            if(_max < arguments[i] {                 =  Arguments[i];             }       }        return _max;}
Value passing

function Plus (num) {       return + +num;} var count = ten; var ret = plus (count);

Results: ret = = = 11; Count = = 10;

function SetName (obj) {       = "Obama";} var president = {name: "Bush"};setname (president);

At this point, obj and President point to the same heap object, and obj replicates only the address in the Stack object, so the result is: {name: "Obama"}.

function overloading

5. RegExp, Date, ErrorRegExp

Defining methods

--/pattern/flags

--New REGEXP (pattern[, flags]);

Test () method

--use regular expressions to test strings and return test results

var reg =/^abc/i;  // start with ABC, ignoring the case reg.test ("Abc123");  // truereg.test ("Ferabc");  // false
Date

Defining methods

var New Date (); var New Date (2018, 3, 10, 7, 1, 1, 100);
6. Math, JSONMath

Object methods

 //  floor: Rounding down  math.floor (0.97); //  0   Math.floor (-5.1); // -6  //  ceil: Rounding up   Math.ceil (1.2); //  2  //  round: rounding    Math.Round (3.4); //  3  //  random: Takes a stochastic number and returns the floating-point numbers between 0~1   Math.random (); // 0.4534623634643   Math.random (); // 0.2048253468450  
JSON

--for storing and exchanging textual information

Json.stringify

--Serializing a JSON object into a string

var json = {1:1, 2: ' 2 ', 3:{4:4, 5:{6:6}}; Json.stringify (JSON);    //
Json.parse

--Converts a JSON string into a JSON object

7. Global Objects
    • . Non-numeric value NaN

--Indicates the result of an error or meaningless operation

--Nan participates in the Operation return is still Nan

-Nan is not equal to any value, including its own

--You can use isNaN () to determine whether the result of the operation is Nan

    • parseint

--Convert a string to a number

    • Encodeduricomponent

--Used to encode Chinese, special characters, etc. in the URI parameter as part of the URI

var url = "http://www.163.com/reg.html?name=" + encodeduricomponent (name);

JS built-in 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.