Constructors and class functions

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    
</body>
</html>
<script>
function People(name,age){
    this.name = name;
    this.age = age;
}
People.info = 'bb'; // constructor directly mounted on, it is the static property
People.prototype.say=function(){
    the console.log ( 'This is an instance attribute People')
}
let p1 = new People ( 'WangShaoQun', 22);
console.log(p1);
console.log (p1.name); // out by the new instance, access to the property called instance attribute;
console.log(p1.age);
p1.say (); // this is an example of a method
console.log (People.info) // static properties: the constructor, direct access to the property, known as static properties.
</script>
 
————————————————————————————————————————————————————————————————
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    
</body>
</html>
<script>
    // create a class of animals
class Animal{
    // This is the role of the constructor of the class constructor: Whenever new this class, the constructor is bound to execute code
    constructor(name,age){
        this.name=name;
        this.age=age;
    }
    static info = 'gender'; // within the class, with static property called static properties modified;

    jion () {
        the console.log ( 'This is an instance attribute Animal');
    }

    static jiao(){
        the console.log ( 'This is a static method Animal'); // within the class, with static property called modified static method;
    }
}
const dog = new Animal ( 'rhubarb', 2);
console.log(dog);
console.log(dog.name);
console.log(dog.age);
dog.jion();
console.log(Animal.info);
Animal.jiao();
</script>

Guess you like

Origin www.cnblogs.com/manban/p/12038016.html