JS Instance properties and prototype properties

Source: Internet
Author: User
Tags access properties

<! DOCTYPE html>
<meta charset= "UTF-8" >
<title> Test Documentation </title>
<script type= "Text/javascript" >



The property is essentially the same as the method, and the method is a function of the reference type.
There are 4 kinds of attributes for an object:
1, the constructor defines the property through the This keyword
2, the constructor defines the property through the VAR keyword
3. Properties added by the constructor's prototype object
4, object dynamically added properties
Public properties of the instance: 1 the properties defined by the This keyword can be accessed 1,2,3,4
Private properties of the instance: 2 properties defined by the VAR keyword. Access to 2
Shared properties for instance: 3 properties added by the prototype to which the instance points. Access to 1,3,4
Static property of the instance: 4 The property that the object is dynamically added. 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.
Dynamically added properties are public properties. Private Property not accessible

The prototype property that the instance object points to is the prototype property. No access to private properties, lower priority than public properties

Instance properties consist primarily of public and privileged attributes. can be accessed by both external and prototype properties. The main difference is whether you can access private properties
Prototype attribute precedence is lower than instance properties. can be accessed by external access and instance properties (except private properties)


-----------------here is the split line-----------------------------
Public property: The property that the object exposes to the external environment. is also the property of the object.
Private properties: Properties inside an object are often inaccessible. It makes sense to consider at the constructor level.
Static properties: Dynamically added properties. is also the property of the object.
Common Properties: Properties that are shared by all constructors that are generated by the instance.

function User () {
Public property: A property for each new user instance object.
For instance properties, the properties of all instances do not share memory.
Externally accessible.
This.name= ' Byronvis ';
Privileged methods: Every new user instance object has a method.
For instance methods, all instances of the method do not share memory.
Externally accessible.
Public properties can be accessed.
You can access private properties.
This.sayname=function () {
alert (this.name);
alert (This.school);
alert (age);//variable declaration is automatically advanced.
alert (this.sex);
};
Private property: External unreachable.
Only meaningful for constructors, no meaning for new's user instance object.
var age=22;
Private method: externally inaccessible.
Only meaningful for constructors, no meaning for new's user instance object.
function Sayage () {
alert (age);
}
Sayage ();
}
Common properties: Shared memory.
User.prototype.school= ' Zky ';
Common methods: Public properties can be accessed.
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= ' man ';
Static method: is a dynamically added instance method.
Obj.saysex=function () {
alert (this.sex);
alert (this.name);
alert (This.school);
alert (age);
};
-----------------here is the split line-----------------------------
The properties that are defined by the This keyword are the same as the properties that are dynamically added, and can be considered the public properties of the instance object.
Validation: The This keyword defines properties that access dynamically added properties
Obj.sayname ();//true
Validation: Dynamically added properties access properties defined by the This keyword
Obj.saysex ();//true
Validation: Public property access Private property
Obj.sayname ();//true
Obj.saysex ();//false
Verify: Shared Properties Access private properties
Obj.sayschool ();//false

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

JS Instance properties and prototype properties

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.