js 类的简单实现与调用
2016-06-15 20:26
316 查看
js类的简单实现与调用
先上一段小代码.里面有注释.
<html> <script> function people(name) { this.name = name; this.version = 0; //展示name this.showName = function() { alert('my name is ' + this.name); } //设置name this.setName = function(name) { this.version++; this.name = name; } //获取name this.getName = function() { return this.name; } } //这种形式定义的方法不能使用类的属性 people.run = function() { alert('I can run'); } //原型方法,可以使用类的属性 people.prototype.showMyName = function() { if(this.version > 0) str = '我改名字了。现在叫 ' + this.name; else str = '我的名字叫 ' + this.name; alert(str); } function dog() { this.name = false; this.master = false; this.setName = function(name) { this.name = name; } } dog.prototype.getName = function() { return this.name; } dog.prototype.setMasterName = function(master) { this.master = master; } dog.prototype.getMasterName = function() { return this.master; } //测试 var p = new people('Jim'); p.showName(); var d = new dog(); d.name = '旺财'; alert(d.getName()); d.setMasterName(p.getName()); alert(d.getMasterName()); alert(d.name + ' 的主人是 : ' + d.master); alert(d.getName() + ' 的主人是 : ' + d.getMasterName()); </script> </html>
js里面没有严格的类.只有一层一层的function.理论上有无限层.
js继承
<script> function base(aa) { this.aa = aa; function get_aa() { return this.aa; } } function sun() { function show() { return 'I am sun show'; } } sun.prototype=new base('hello world'); alert(sun.get_aa()); //输出 hello world alert(sun.show()); //输出 I am sun show </script>
转载于:https://my.oschina.net/u/2338485/blog/692588
相关文章推荐
- 用js调用WebService简单实现
- js 类的简单实现与调用
- 使用DWR实现JS调用JAVA类的简单例子
- 原生js方式实现ajax,并仿jquery方式简单调用
- dwr+maven实现java和js方法互相调用,实现推送, 完整切超简单例子
- JS调用百度api接口——实现简单的百度页面
- [原](源码)JS OOP 实现页面浮动图片,支持多图同时浮动,简单调用
- 用js调用WebService简单实现
- 学习Altas 笔记[JS简单调用服务端方法]
- 简单js实现统计图(平面图、柱状图、圆形)
- 学习Altas 笔记[JS简单调用服务端方法]
- (转)母版页调用内容页的方法的简单实现
- html中用js调用ASP文件 实现静态页面动态显示
- 一个简单的使用SOAP协议调用Webservice实现
- 一个简单的oracle分页存储过程的实现和调用
- 用最简单的办法,实现最好的打印(用BCB调用记事本的打印功能)
- 不用AJAX实现前台JS调用后台C#方法(小技巧) (转)
- 一个简单的js实现的隔行变色脚本
- js实现一个简单登陆框
- JS调用C#后台代码---JS实现DataGrid“全选”、“反选”、调用后台代码批量删除数据