Call of built-in object prototype

Source: Internet
Author: User

The publisher statement in publish/subscribe mode converts arguments to an array.

VaR ARGs = array. Prototype. Slice. Call (arguments, 0 );

This kind of built-in object prototype call can be used;

 

The "internal object prototype cannot be modified" as required in the encoding specification is similar to the following:

String.prototype.startsWith = function(text) {            return this.indexOf(text) == 0;        }

 

Arguments is an object.. It has [0], [1], length, which cannot represent an array, and it has other things.

Array arguments

 

function fn() {        console.log(typeof arguments);   //object        console.log(arguments instanceof Array); //false        for(var i = 0; i < arguments.length;i++) {            console.log(arguments[i]);        }        var arr = [1,2,3];        console.log(arr);    }

 

There isForeachUsage

function showResults(value, index) {        console.log(‘value:‘ + value);        console.log(‘index:‘ + index);    }
var arr = [‘nihao‘, ‘nibuhao‘, ‘nihaobuhao‘];
arr.forEach(showResults);

//console.log

Value: nihao
Index: 0
Value: nibuhao
Index: 1
Value: nihaobuhao
Index: 2

 

 

Arguments cannot use foreach

     function fn() {        arguments.forEach(showResults);    }    function showResults(value, index) {        console.log(‘value:‘ + value);        console.log(‘index:‘ + index);    }    fn(‘nihao‘, ‘nibuhao‘);    //Uncaught TypeError: undefined is not a function 

 

Call of built-in object prototype

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.