Return objects to enable chaining of functions

来源:互联网 发布:c语言程序代码大全 编辑:程序博客网 时间:2024/06/13 13:25

When creating functions on an object in Object Oriented Javascript, returning the object in the function will enable you to chain functions together.

function Person(name) {  this.name = name;  this.sayName = function() {    console.log("Hello my name is: ", this.name);    return this;  };  this.changeName = function(name) {    this.name = name;    return this;  };}var person = new Person("John");person.sayName().changeName("Timmy").sayName();

from:github/loverajoel

0 0
原创粉丝点击