js之面向对象

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>

<script>

    function foo() {
        var xo='randomlee';
        console.log(xo);

    }


    foo();


    function Foo(n) {
        this.name=n;
    }
    var obj=new  Foo('randomlee');

    console.log(obj.name);




    // function Foo(n){
    //     this.name=n;
    //     this.sayName=function(){
    //         console.log(this.name)
    //
    //     }
    //
    //
    // }

    //  原型  对原有的函数进行操作 在实例化函数的时候并不实例化原型部分 在调用的时候才去查找 节约内存
    Foo.prototype={
        'sayName':function () {
            console.log(this.name);
        }
    }

    var obj1=new Foo('random');

    obj1.sayName()


</script>
</html>

  

原文地址:https://www.cnblogs.com/randomlee/p/9826919.html