Java Script class definitions of classes and objects

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <script type="text/javascript">
            class Cat{
                // special method (constructors) constructor to create and initialize a class object is created 
                constructor () {
                     the this .name = "cat"
                     the this 18 is .age =
                     // Sound method every time an object is created to instantiate time, will take up more memory space, it is best not to write in the constructor 
                    the this .sound = function () {
                        console.log ( "Oh, would call" )
                    }
                    
                }
                // write out of constructor, would not each instantiation will be created once, saving space, can be found in the prototype 
                run () {
                        console.log ( "will run oh" )
                    }
            }
            var cat1=new Cat()
            console.log(cat1)            
        </script>
    </body>
</html>

 Using inheritance

class BSCat extends Cat{
                constructor(){
                    super()
                    this.type="bscat"
                    
                }
                layout () {
                    console.log ( "cute" )
                }
            }
var bscat=new BSCat()
            console.log(bscat)

 

Guess you like

Origin www.cnblogs.com/a155-/p/12233646.html