js单列和继承的一些写法
来源:互联网 发布:java微信分享接口开发 编辑:程序博客网 时间:2024/05/29 18:13
------------------------
<script type="text/javascript">
/**
* @descriptor 使用这样的继承方式是有问题的,books这种类型的变量是所有对象共享的啦,不会在自己的空间中创建book的空间
* @param name
* @param age
* @constructor
*/
var Person = function(name,age){
this.name =name;
this.age = age;
this.books = [];
}
Person.prototype.say=function(){
alert(this.name+","+this.age+this.books);
}
Person.prototype.getBooks=function(){
return this.books;
}
var Student = function(){}
Student.prototype = new Person();
var s = new Student();
s.getBooks().push("java");
s.say();
var s2 = new Student();
s2.getBooks().push('javascript');
s2.say();
</script>
-----------------------解决办法--------------------------------
<script type="text/javascript">
/**
* @descriptor 使用这样的继承方式是有问题的,books这种类型的变量是所有对象共享的啦,不会在自己的空间中创建book的空间
* @param name
* @param age
* @constructor
*/
var Person = function(name,age){
this.name =name;
this.age = age;
this.books = [];
}
Person.prototype.say=function(){
alert(this.name+","+this.age+this.books);
}
Person.prototype.getBooks=function(){
return this.books;
}
var Student = function(name,age,no){
Person.call(this,name,age);//这句话相当于
/*this.name =name;
this.age = age;
this.books = [];*/
}
Student.prototype = new Person();
var s = new Student();
s.getBooks().push("java");
s.say();
var s2 = new Student();
s2.getBooks().push('javascript');
s2.say();
</script>
----------------------------解决方案2------------------------------------
<script type="text/javascript">
function ExtndsUtils(SubClass,SuperClass){
var temp = function(){};
temp.prototype = SuperClass.prototype;
SubClass.prototype = new temp();
SubClass.superClass = SuperClass.prototype;
}
</script>
<script type="text/javascript">
/**
* @descriptor 使用这样的继承方式是有问题的,books这种类型的变量是所有对象共享的啦,不会在自己的空间中创建book的空间
* @param name
* @param age
* @constructor
*/
var Person = function(name,age){
this.name =name;
this.age = age;
this.books = [];
}
Person.prototype.say=function(){
alert(this.name+","+this.age+this.books);
}
Person.prototype.getBooks=function(){
return this.books;
}
var Student = function(name,age,no){
Student.superClass.constructor.call(this,name,age);//这句话相当于
/*this.name =name;
this.age = age;
this.books = [];*/
}
ExtndsUtils(Student,Person);
var s = new Student();
s.getBooks().push("java");
s.say();
var s2 = new Student();
s2.getBooks().push('javascript');
s2.say();
</script>
--------------------------单列的定义方式-----------------------------------------
/**
* Created by canton_cowboy on 14-7-13.
*/
/**
* 一般的单列定义方式
*/
var MyNameSpace={}
MyNameSpace.test1=(function(){
/**
* 私有的属性和方法一般都是以_开头
*/
function _privF1(){
}
function _privF2(){}
return {
pubF:function(){
//私有方法是外面无法访问的
_privF1();
_privF2();
}
}
})();
0 0
- js单列和继承的一些写法
- swift中单列的写法
- js 的一些写法须知
- 一些‘优雅’的js写法
- Js组件的一些写法
- js的一些兼容性写法
- js下IE和FF的一些兼容写法总结
- React.js中ES6 和 ES5写法的一些差别
- cocos2dx 3.0 js继承:John Resiq的继承写法解析
- cocos2dx 3.0 js继承:John Resiq的继承写法解析
- cocos2dx 3.0 js继承:John Resiq的继承写法解析
- 注册验证的js的一些写法
- js中的一些写法
- JS一些兼容性 写法
- Java面向对象 继承类的一些特殊写法
- 官方单列写法
- 单列设计模式几种写法的比较
- 关于js继承的一些代码
- locate find whereis which type
- 操作系统文件系统综合设计
- 把简单的matlab/Octave脚本转换成python脚本的工具
- WebView示例
- ubuntu下shell命令没有了的解决方法
- js单列和继承的一些写法
- Longest Palindromic Substring
- python每次处理一个字符(读书笔记)
- 【HDU】3342 Legal or Not 拓扑排序
- day119V20140710
- ssl协议是什么
- Android环境变量的设置(附详细图)
- 正则表达式里字符串”不包含”匹配技巧
- 软件开发的21条规律