js对象构造函数用法

//原理要点,Function也是一个对象 

    $(function () {

        var Person = function (name) {
            this.name = name;
            //唱歌
            this.singSong = function () {//这种方式会在每次实例化的时候在内存中划分一块新的空间存储函数
                //我会唱歌,不是每个人都会,所以我不是公共方法
                return 'name is '+ this.name;
            }
        };
        //此方式可以达到“继承”的效果,同时会节省内存空间
        Person.prototype.go = function(name){
            console.log('我是'+name+',我会走路,我是公共方法');
        }

        var p = new Person('李四');
        var s = p.say();
        console.log(s);

        var p2 = new Person('王五');
        var s2 = p2.say();
        console.log(s2);

    });

可以完成不同参数输出不同值。

你可能感兴趣的:(js)