js学习笔记01
2014-09-11 10:52
260 查看
1 传值和传址:
js的数据赋值根据数据类型来判别,
传值:数据值,布尔型,字符型基本数据
传址:数据,hash对象,赋值的过程会用内存地址赋值,影响原数据。解决方案:遍历数组和对象赋值,相当于传值。
prototype也是hash对象,赋值的话也会传址.
解决方案:
(1)上述类似的,遍历赋值
(2)把bird作为Animal的一个实例
(3)把方法封装起来
2 数据类型
undefined,null,"",0,转换为逻辑值为false,其他(简单类型,对象,函数)都为true。在这五个之中,undefined==null为true,其实均为false;
js的数据赋值根据数据类型来判别,
传值:数据值,布尔型,字符型基本数据
传址:数据,hash对象,赋值的过程会用内存地址赋值,影响原数据。解决方案:遍历数组和对象赋值,相当于传值。
prototype也是hash对象,赋值的话也会传址.
function Animal(name){ this.name=name; this.type="animal"; } Animal.prototype.say=function(){ console.log(this.name+"type is:"+this.type); } function Bird(name){ Animal.call(this,name); } Bird.prototype.fly=function(){ console.log("I'm flying"); } Bird.prototype=Animal.prototype; var mybird=new Bird("bird"); var mydog=new Animal(); mybird.fly();//I'm flying mydog.fly();//I'm flyingAnimal的prototype也会带有fly()方法。
解决方案:
(1)上述类似的,遍历赋值
function Animal(name){ this.name=name; this.type="animal"; } Animal.prototype.say=function(){ console.log(this.name+"type is:"+this.type); } function Bird(name){ Animal.call(this,name); } for(var p in Animal) { Bird.prototype[p]=Animal.prototype[p]; } Bird.prototype.fly=function(){ console.log("I'm flying"); } var mybird=new Bird("bird"); var mydog=new Animal(); mybird.fly();//I'm flying mydog.fly();// Uncaught TypeError: undefined is not a function
(2)把bird作为Animal的一个实例
function Animal(name){ this.name=name; this.type="animal"; } Animal.prototype.say=function(){ console.log(this.name+"type is:"+this.type); } function Bird(name){ Animal.call(this,name); } Bird.prototype=new Animal(); Bird.prototype.constructor=Bird; Bird.prototype.fly=function(){ console.log("I'm flying"); } var mybird=new Bird("bird"); var mydog=new Animal(); mybird.fly();//I'm flying mydog.fly(); // Uncaught TypeError: undefined is not a function
(3)把方法封装起来
function extend(subClass,superClass){ var F=function(){}; F.prototype=superClass.prototype; subClass.prototype=new F(); subClass.prototype.contructor=subClass; } function Animal(name){ this.name=name; this.type="animal"; } Animal.prototype.say=function(){ alert(this.name+"type:"+this.type); } function Bird(name){ this.name=name; this.type="bird"; } extend(Bird,Object); Bird.prototype.fly=function(){ alert("I'm flying"); } var canary=new Bird("xiaocui"); var animal= new Animal(); canary.say(); canary.fly();
2 数据类型
undefined,null,"",0,转换为逻辑值为false,其他(简单类型,对象,函数)都为true。在这五个之中,undefined==null为true,其实均为false;
相关文章推荐
- 【06-23】js动画学习笔记01
- node.js学习笔记-01 events
- day02_js学习笔记_01_js的简介、js的基本语法
- Node.js学习笔记 01 搭建静态服务器
- [学习笔记01]js基础变量及数据类型
- 01js学习笔记
- js权威指南---学习笔记01
- JS学习笔记01-创建对象
- JS宝典学习笔记(转)
- [转]JS宝典学习笔记
- JS学习笔记
- js学习笔记(一)
- JS宝典学习笔记(下)
- ajaxPro /prototype.js 学习笔记
- C#设计模式(学习笔记[01])
- 学习altas笔记[客户端JS和Altas环境初始化关系和DataTable返回数据的客户端处理]
- 学习Altas 笔记[js调用重载的方法出错,如何处理]
- js宝典学习笔记【转载】
- 学习Altas 笔记[JS简单调用服务端方法]
- 学习altas笔记[客户端JS和Altas环境初始化关系和DataTable返回数据的客户端处理]