链式调用 其实就是把本身的值返回出去,供下次调用使用。

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>链式调用</title>
    7. </head>
    8. <body>
    9. </body>
    10. </html>
    11. <script>
    12. function Person(){
    13. this.value = '前,端,伪,大,叔';
    14. }
    15. Person.prototype.split = function () {
    16. return this.value.split(',');
    17. }
    18. Person.prototype.reverse = function () {
    19. if(!Array.isArray(this.value)) return 'not Array'
    20. this.value.reverse();
    21. return this;
    22. }
    23. const ming = new Person();
    24. console.log(ming.split()); // (5) ["前", "端", "伪", "大", "叔"]
    25. console.log(ming.split().reverse()); // (5) ["叔", "大", "伪", "端", "前"]
    26. console.log(ming.reverse()); // not Array
    27. console.log(ming.value); // 前,端,伪,大,叔
    28. </script>