JavaScript class methods, object properties, and the relevant understanding of prototype methods

Source: Internet
Author: User

1, Object methods, properties: Is the method at the object instance level, did not create an instance, this instance has the corresponding object method, the instance can use the object method.

Eg:function people (name) {

Object properties, public (that is, each instance has one of this property or method)

THIS.name = name;

Object method, Public

This.introduce = function () {

Alert (' My name = ' +this.name ');

};

}

2, class methods, properties (static methods, properties): The function at the class level, do not need to generate an instance of the existing properties, in memory to open a copy.

Called directly using the (class name. property). Use (class name. Property = .... Create

//公有静态属性 在类的外部

//公有静态属性不能使用 【this.属性】,只能使用 【对象.属性】 调用

Eg: Connect to Person:

Person.run = function () {

Alert (' This is a static method (class method) ');

}

Person.sex = "This is a static property";

3, the prototype method: Add a property (method) to the prototype of the class, by the prototype added, in the subsequent creation of the instance is a share of the code, in memory only one copy.

( Adding this method to the prototype will enable sharing.) This makes it necessary to allocate the appropriate memory for each instance when it is initialized. )

//原型属性,当作是类内部的属性使用【this.原型属性】,也可以当成公有静态属性使用【对象.prototype.原型属性】

Use (class name. prototype. Properties = ... ) Add Create,

Called through an instance.

Eg:Person.prototype.introduceChinese = function () {

Alert (' This is the prototype method ');

}

======= I'm a split line ==============================================================================

Differences and understandings of three method attributes:

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>test</title>
<body>
<script>
Object Constructors
function Atest (name) {
Private properties, which can only be used inside the object constructor
var className = "Atest";
Public property, called after an object instantiation
THIS.name = name;
Object methods
This.hello = function () {
alert (this.name);
Alert (this.msg ());//The method of extending using the prototype method can be used inside the class
alert (this.sex);//Properties extended using the prototype method can be used inside the class
alert (atest.age);//static property is called when the format is [object. Static property]
}
}
The location of the class method (which is actually called directly by a static method): The external syntax format for the Person class: Class name. Method name = function ([parameter ...]) {statement line;}
Atest.run = function () {
Alert ("I am the class method Run");
}


Prototyping methods
Atest.prototype.msg = function () {
Alert ("My name is:" +this.name);//If the prototype method is called directly as a static method, THIS.name cannot be called
}

public static properties outside of class
Atest.age = 20;//public static property cannot use "this. Property" and can only be called with "object. Properties"

The prototype property, which is considered to be a property inside the class, uses the "this. prototype attribute", or it can be used as a public static property "object. prototype. Prototype Properties"
Atest.prototype.sex = "male";

Atest.run (); Class methods are also static methods that you can use directly with the object. static method ()
Atest.prototype.msg ();//The prototype method is used as a static method when using the object. Prototype. Method () "
alert (Atest.prototype.sex);//The prototype property is used as a static property when you use the object. Prototype. Method () "
var a = new Atest ("Zhangsan");//Object methods and prototype methods need to instantiate objects before they can be used
A.hello ();//object method must instantiate object
A.msg ();//The prototype method must instantiate the object
Alert (A.age)://error, public static property can only be called with "object. Property"

PS: As far as possible the method is defined as a prototype method, the prototype method avoids each call to the constructor of the property or method construction, save space, create objects faster.
</script>
</body>

JavaScript class methods, object properties, and the relevant understanding of prototype methods

Related Article

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.