標籤:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div>
<input type="text" id="content">
<button type="button" id="btn">click</button>
</div>
<script>
window.onload = function(){
var oContent = document.getElementById(‘content‘);
var oBtn = document.getElementById(‘btn‘);
var SayHello = function(){
this.content = ‘‘;
};
SayHello.prototype = {
SayMorning: function(){
this.content += ‘good morning‘;
//每次調用返回this也就是調用函數的對象
return this;
},
SayNight: function(){
this.content += ‘good night‘;
return this;
}
}
oBtn.addEventListener(‘click‘, function(){
var sayhello = new SayHello();
//在這裡地阿姨那個SayMorning之後返回了調用對象sayhello
sayhello.SayMorning().SayNight();//這裡相當於sayhello.SayMorning(),sayhello.SayNight();
oContent.value = sayhello.content;
},false);
}
</script>
</body>
</html>
用原生js實現的鏈式調用函數