码迷,mamicode.com
首页 > Web开发 > 详细

用原生js实现的链式调用函数

时间:2016-10-12 11:23:10      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

<!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实现的链式调用函数

标签:

原文地址:http://www.cnblogs.com/Upton/p/5951739.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!