链式调用 其实就是把本身的值返回出去,供下次调用使用。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>链式调用</title>
</head>
<body>
</body>
</html>
<script>
function Person(){
this.value = '前,端,伪,大,叔';
}
Person.prototype.split = function () {
return this.value.split(',');
}
Person.prototype.reverse = function () {
if(!Array.isArray(this.value)) return 'not Array'
this.value.reverse();
return this;
}
const ming = new Person();
console.log(ming.split()); // (5) ["前", "端", "伪", "大", "叔"]
console.log(ming.split().reverse()); // (5) ["叔", "大", "伪", "端", "前"]
console.log(ming.reverse()); // not Array
console.log(ming.value); // 前,端,伪,大,叔
</script>