JS Learning (v)-global function and class intrinsic function difference

Source: Internet
Author: User

---------------------------JS Code---------------------------

function User () {
Definition and constructors of class members
this.name = "Hello";
This.age = 25;
This.sayhello = function () {
Console.log ("Here is Class1");
}
}

-------------------------HTML page-------------------------

<script type= "Text/javascript" src= "Demo01.js" ></script>
<script type= "Text/javascript" >
var u = new User ();
var say = new U.sayhello ();
Console.log (typeof (say))
</script>

Summary: There is no difference between a global function and a function defined as an object method, because global functions and variables can be thought of as methods and properties of the Window object. You can also use the new operator to manipulate an object's methods to return an object, so that the method of an object can be defined as the form of a class, where the this pointer points to the newly created object.

JS Learning (v)-global function and class intrinsic function difference

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.