Please read the comments carefully for details. Let’s talk less nonsense here and just upload the code.
The code copy is as follows:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test Document</title>
<script type="text/javascript">
// In essence, the properties and methods are the same, and the methods are functions whose properties are reference-type.
// An object has 4 properties:
// 1, the attributes defined by the constructor through this keyword
// 2, the attributes defined by the constructor through the var keyword
// 3, Properties added to the constructor's prototype object
// 4, the attributes added dynamically by the object
//Public properties of the instance: 1, 2, 3, 4 can be accessed through the attributes defined by this keyword
//Private attributes of the instance: 2 attributes defined by the var keyword. Accessible 2
//Share properties of an instance: 3 Attributes added through the prototype pointed to by the instance. Accessible 1,3,4
//Static properties of the instance: 4 properties added dynamically by the object. Accessible 1,3,4
//Summarize:
// Instance attributes: 1, public
// 2, private
// 4, static
// Prototype attributes: 3, share
//This is defined as a privileged attribute. All accessible
//Var is defined as a private property.
//The attributes added dynamically are public attributes. Private properties are not accessible
//The prototype attribute pointed to by the instance object is the prototype attribute. Private attributes are not accessible, with lower priority than public attributes
//Instance attributes mainly consist of public attributes and privilege attributes. Both can be accessed by external and prototype properties. The main difference is whether private attributes are accessible
//The precedence of the prototype attribute is lower than that of the instance attribute. Can be accessed externally and instance attributes (except private attributes)
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Public properties: attributes exposed to the external environment. It is also an attribute of the object.
// Private attributes: Attributes inside objects are often inaccessible. It is only meaningful to consider them at the constructor level.
// Static properties: dynamically added properties. It is also an attribute of the object.
// Shared properties: attributes shared by all instances generated by constructors.
function User(){
// Public properties: Attributes that each new User instance object has.
// is an instance attribute, and all instance attributes do not share memory.
// Accessible externally.
this.name='byronvis';
// Privileged method: A method that has been used for each new User instance object.
// is an instance method, all instance methods do not share memory.
// Accessible externally.
// Public properties are accessible.
// Private properties are accessible.
this.sayName=function(){
alert(this.name);
alert(this.school);
alert(age);//Variable declaration will be automatically advanced.
alert(this.sex);
};
// Private attributes: Not accessible externally.
// It only makes sense to constructors, not to new User instance objects.
var age=22;
// Private method: Not accessible externally.
// It only makes sense to constructors, not to new User instance objects.
function saysAge(){
alert(age);
}
sayAge();
}
// Shared attributes: shared memory.
User.prototype.school='zky';
// Common methods: public attributes 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 properties: is the instance properties added dynamically.
obj.sex='man';
// Static method: is the instance method added dynamically.
obj.saySex=function(){
alert(this.sex);
alert(this.name);
alert(this.school);
alert(age);
};
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//// Prove that the attributes defined by this keyword are essentially the same as those added dynamically, and can be considered as public attributes of instance objects.
// Verification: This keyword defines properties to access dynamically added properties
// obj.sayName();//true
// Verification: Dynamically added attributes access the attributes defined by this keyword
// obj.saySex();//true
// Verification: Public properties access private properties
// obj.sayName();//true
obj.saySex();//false
// Verification: Shared attributes access private attributes
// obj.saySchool();//false
</script>
</head>
<body>
Test Documents
</body>
</html>
Have you understood the example attributes and prototype attributes? This article is very detailed and recommended to everyone. I hope it will be helpful to my friends.