------------------------
<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();
}
}
})();