JS Instance properties and Prototype properties sample detailed _javascript Tips

Source: Internet
Author: User
Tags access properties instance method

For details, please read the notes carefully, here is a little more nonsense, directly on the code.

Copy Code code as follows:

<! DOCTYPE html>
<meta charset= "UTF-8" >
<title> Test Documentation </title>
<script type= "Text/javascript" >
Essentially, the property and method are the same, and the method is a function that is a reference type of the property.
An object has 4 properties:
1, the property defined by the constructor through the This keyword
2, properties defined by the constructor through the VAR keyword
3, the constructor's prototype object adds a property
4, the dynamically added properties of the object
Public properties of instance: 1 properties defined by this keyword are accessible to 1,2,3,4
Instance's private property: 2 properties defined by the VAR keyword. Access to 2
Instance's shared properties: 3 properties added by the prototype to which the instance points. Access to 1,3,4
Instance's static property: A property added dynamically by the 4 object. Access to 1,3,4

Summarize:
Instance properties: 1, public
2, private
4, Static
Prototype properties: 3, shared

This is defined as a privileged property. All accessible
var is defined as a private property.
The dynamically added property is a public property. No access to private properties

The prototype attribute that the instance object points to is a prototype property. Non-access private property, priority lower than public property

Instance properties are mainly composed of public and privileged attributes. can be accessed by both external and prototype properties. The main difference is whether you can access private properties
The stereotype attribute has precedence over the instance property. can be accessed by external access and instance properties (except private properties)


-----------------here for the split line-----------------------------
Public properties: Objects are exposed to the properties of the external environment. is also an object's property.
Private properties: Properties within an object are often inaccessible. It is meaningful to consider at the constructor level.
Static properties: Dynamically added properties. is also an object's property.
Common Properties: Properties that are shared by instances that are generated by all constructors.

function User () {
Public properties: Each new user instance object, all properties.
For instance properties, the properties of all instances do not share memory.
Externally accessible.
This.name= ' Byronvis ';
Privilege method: Each new user instance object, all methods.
For instance methods, the methods of all instances do not share memory.
Externally accessible.
can access public properties.
Private properties can be accessed.
This.sayname=function () {
alert (this.name);
alert (This.school);
alert (age);//The variable declaration is automatically advanced.
alert (this.sex);
};
Private property: External inaccessible.
is meaningful only to constructors, and is meaningless for the user instance object of new.
var age=22;
Private method: External inaccessible.
is meaningful only to constructors, and is meaningless for the user instance object of new.
function Sayage () {
alert (age);
}
Sayage ();
}
Common properties: Shared memory.
User.prototype.school= ' Zky ';
Common method: Access to public properties.
Shared memory.
User.prototype.sayschool=function () {
alert (This.school);
alert (this.name);
alert (this.sex);
alert (age);
};
var obj=new User ();
Static property: Is the dynamically added instance property.
Obj.sex= ' Mans ';
Static method: Is the dynamically added instance method.
Obj.saysex=function () {
alert (this.sex);
alert (this.name);
alert (This.school);
alert (age);
};
-----------------here for the split line-----------------------------
Prove that the This keyword defines a property that is essentially the same as a dynamically added property, and can be considered a public property of an instance object.
Validation: This keyword defines properties that access dynamically added properties
Obj.sayname ();//true
Validation: Dynamically added property access properties defined by this keyword
Obj.saysex ();//true
Authentication: Public Property access Private property
Obj.sayname ();//true
Obj.saysex ();//false
Authentication: Shared Properties Access private properties
Obj.sayschool ();//false

</script>
<body>
Test document
</body>

Do the small partners see it, understand the instance properties and the prototype properties? This article describes the very detailed, recommended to everyone, I hope that the small partners can help

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.