Sample js instance properties and prototype properties

  • 2020-03-30 04:20:09
  • OfStack

Please read the comments carefully for more details, so I'll cut the crap here and go straight to the code.


<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title> Test documentation </title>
    <script type="text/javascript">
//Properties are essentially the same as methods, which are functions whose properties are referential. < br / > //An object has four properties:
//                                1, the constructor
by the property defined by this keyword //                                2, the constructor defined by the var keyword property
//                                3, the properties added by the constructor's prototype object
//                                4, object dynamically added properties
//Public property of the instance: 1& NBSP;         Property & PI defined by this keyword;                   To access     1,2,3,4
//Private property of the instance: 2& PI;         Properties defined by the var keyword.                   To access     2 < br / > //The Shared property of the instance: 3& NBSP;         Properties added by the stereotype pointed to by the instance.           To access     1 and 4 < br / > //Static property of the instance: 4& NBSP;         Object dynamically adds properties.                             To access   1 and 4 //Conclusion:
//        Instance property: 1, public
//                          2, private
//                          4, static
//        Stereotype attributes: 3, Shared //This is defined as a privileged property. All accessible
//Var defines a private property. < br / > //Dynamically added properties are public properties. Private properties cannot be accessed //The stereotype properties that the instance object points to are the stereotype properties. Private properties are not accessible and have a lower priority than public properties //Instance attribute mainly consists of public attribute and privilege attribute. Both are accessible by external and stereotype properties. The main difference is whether the private property
is accessible //Stereotype properties have a lower priority than instance properties. Can be accessed externally and by instance properties (except private properties)
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- here for line -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- < br / > //Public attributes: attributes of an object exposed to the external environment. It's also a property of an object. < br / > //Private properties: properties inside an object that are often inaccessible. It makes sense to consider them at the constructor level. < br / > //Static properties: properties that are added dynamically. It's also a property of an object. < br / > //Common properties: properties Shared by all construction-generated instances.        function User(){
//                    Public property: every new User instance object has an attribute. < br / > //                                      Is an instance property, all instance properties do not share memory. < br / > //                                      Externally accessible. < br / >            this.name='byronvis';
//                    Privileged methods: for each new User instance object, there are methods. < br / > //                                      For instance methods, all instance methods do not share memory. < br / > //                                      Externally accessible. < br / > //                                      Public properties are accessible. < br / > //                                      Private properties are accessible. < br / >            this.sayName=function(){
            alert(this.name);
               alert(this.school);
            alert(age);//The variable declaration is automatically advanced. < br / >             alert(this.sex);
           };
//                    Private property: not externally accessible. < br / > //                                      Only makes sense for the constructor, not for the User instance object of new. < br / >            var age=22;
//                    Private method: not externally accessible. < br / > //                                      Only makes sense for the constructor, not for the User instance object of new. < br / >            function sayAge(){
               alert(age);
           }
           sayAge();
       }
//            Shared property: Shared memory. < br / >        User.prototype.school='zky';
//            Common methods: public properties can be accessed. < br / > //                              Shared memory. < br / >        User.prototype.saySchool=function(){
           alert(this.school);
           alert(this.name);
           alert(this.sex);
           alert(age);
       };
        var obj=new User();
//            Static properties: are instance properties that are added dynamically. < br / >         obj.sex='man';
//            Static methods: are dynamically added instance methods. < br / >         obj.saySex=function(){
            alert(this.sex);
            alert(this.name);
            alert(this.school);
            alert(age);
        };
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- here for line -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- < br / > ////          Proving that the properties defined by this keyword are essentially the same as the dynamically added properties can be considered public properties of an instance object. < br / > //            Verify: the properties defined by this keyword access dynamically added properties
//        obj.sayName();//true
//              Validation: dynamically added properties access the properties defined by this keyword
//        obj.saySex();//true
//              Validation: the public property accesses the private property
//        obj.sayName();//true
           obj.saySex();//false
//              Verify: the Shared property accesses the private property
//        obj.saySchool();//false    </script>
</head>
<body>
    Test documentation
</body>
</html>


Related articles: