JS Object-oriented static method and static attribute instance analysis

Source: Internet
Author: User

First look at the following code:

The code is as follows: <script type= "Text/javascript" >
function Bird () {
this.wing = 2;
This.fly = function () {
Alert ("I am a bird, I Can Fly");
}
}
var maque = new Bird ();//After creating the Maque object, we can use Bird methods and properties
</script>


Think: Can we use bird methods and properties without creating a Maque object?

Knowledge points added:

(1) What is a function: A function is a variable, and a function is an object. The essence of a function is actually this:
var sum = new Function (' x ', ' y ', ' return x+y ');//x and y are parameters of the function, and "return x+y" is the function body.

(2) In JS, objects, functions, and arrays are created by the constructor. So, they're all objects. Since it is an object, the function must have attributes and methods.

The code is as follows: <script type= "Text/javascript" >
function Bird () {
this.wing = 2;
This.fly = function () {
Alert ("I am a bird, I Can Fly");
}
}
Bird.jiao = function () {alert (' Twitter called ')};//bird is also an object, so it can have methods
Bird.jiao ()//calling method
</script>

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.