JavaScript function () {},new function (), new function (), function simple Introduction

Source: Internet
Author: User

Tags: Method Understanding Relation Object Notation new introduction Max Tor

A function is a very important language element in JavaScript, and provides a function keyword and built-in object function, following its possible usage and relationships between them.

function how to use
var function ()//or function foo01 () {       var temp = +;        this. temp = $;        return  This . temp;   }   Alert (typeof(foo01));  //  alert (foo01 ());          //

The most common way to use function is to set a JavaScript function. The two kinds of writing show that the effect is exactly the same, but the only one is that the latter is a higher initialization priority. In a variable scope within a large expansion number, this refers to the owner of the Foo01, the Window object.

How to use the new function ()
var New function ()   {       var temp = +;        this. temp = $;        return  This . temp;   }   Alert (typeof(foo02));    //  alert (Foo02.constructor ());   // -   

This is a comparison of the use of puzzle functions, as if a function is set. But this is actually a user-defined object in JavaScript, but this is an anonymous class. This usage is basically not related to the use of the function itself, and a variable scope is constructed in the large expansion number, which refers to the scope itself. (understood to instantiate anonymous classes)

How to use the new Function ()
var New Function (' var temp = n; this.temp = n; return temp + this.temp; ' );   Alert (typeof(Foo3));  // Object Alert (Foo3 ());           //  -

Use the system built-in function object to build a function, and the first way in method one is exactly the same on both the effect and the initialization priority, that is, the function body is given as a string.

Function () How to use
var = Function (' var temp = Foo4; this.temp = $; return temp + this.temp; ' );   Alert (typeof(Foo4));     // function alert (Foo4 ());              //  -

This method is not used frequently, the effect and method are the same, but it is unclear whether new to generate any side effects, this also embodies the JavaScript one of the biggest features: Flexible! Can save on the province. (Not recommended)

JavaScript function () {},new function (), new function (), function simple Introduction

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.

Tags Index: