JavaScript Explore--2

Source: Internet
Author: User
Apply and call usages extend the scope of functions on which they are run
<script>        var color = ‘红色‘;        var box = {            color : ‘蓝色‘        }        function sayColor() {            console.log(this.color);        }        sayColor.apply(this);//this指代window作用域        sayColor.apply(box);//box指代box作用域</script>
Note: Apply is the same as call usage, except that the parameters passed are different
<script>    function sum(num1, num2) {        return num1 + num2;    }    function add(num1, num2) {        return sum.apply(this, arguments);//apply传递参数可以用arguments对象代替        return sum.call(this, num1, num2);//call传递参数必须每个都写    }    add(10,10);</script>

JavaScript Explore--2

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.