美文网首页
JavaScript基础 面向对象 链式操作

JavaScript基础 面向对象 链式操作

作者: 0说 | 来源:发表于2018-03-28 01:25 被阅读0次

链式操作

        function Fn(  ) {
            this.name = '阿里';
            this.age = '18';
        }
        Fn.prototype.sayHi = function (  ) {
            console.log( 'hi' )
            return this;     //这里的this指向new 出来的对象
        }
        Fn.prototype.sayHello = function (  ) {
            console.log( 'Hello' )
            return this;     //这里的this指向new 出来的对象
        }
        Fn.prototype.getName = function (  ) {
            console.log( this.name )
            return this;     //这里的this指向new 出来的对象
        }
        Fn.prototype.getAge = function (  ) {
            console.log( this.age )
            return this;     //这里的this指向new 出来的对象
        }
        var obj = new Fn();
        obj.sayHi().sayHello().getName().getAge();
        //obj.sayHi() 这里执行返回的是undefined 所有后面就报错  如果要可以 我们想办法把对象返回出来
image.png

相关文章

网友评论

      本文标题:JavaScript基础 面向对象 链式操作

      本文链接:https://www.haomeiwen.com/subject/jbiecftx.html