JS语法:


JS知识点一览图

JS知识点一览图

在function中写this,在本function中可以显示,写Person则显示undefined.
在function中写Person,在function外可以显示,写this则显示undefined.
var name;
var age;
var info;
function Person(name,age){
this.name=name;
Person.age=age;
this.info=function(){
document.writeln("name is:"+this.name+"<br/>");
document.writeln("age is:"+this.age+"<br/>");
};
}
var p=new Person('chen',22);
p.info();
document.writeln("w-name is:"+this.name+"<br/>");
document.writeln("w-age is:"+Person.age+"<br/>");
用call方法遍历数组:
var each=function(array,fn){
for(var index in array){
fn.call(window,index,array[index]);
}
}
each([4,20,3],function(index,ele){
document.write("第"+index+"个元素是:"+ele+"<br/>");
});
全局变量在函数里用this取不到,要用window调用。
    var scope="全局变量" ;

    function test(){

        document.writeln(scope+"<br/>");
document.writeln(window.scope+"<br/>");
var scope="局部变量";
document.writeln(scope+"<br/>");
}
test();
如果定义的函数没有明确赋值给某个对象,则用window调用 。
var hello=function(name){
document.write(name+"say:hello<br/>");
}
window.hello("chen");
继承
function Base(){
this.member="em";
this.method=function(){
window.alert(this.member);
}
}
function extend(){
Base.call(this);
window.alert(member);
window.alert(this.method);
}
extend();
继承进阶:输出的是Cat,执行的是Animal里的showName。
function Animal(){
this.name='Animal';
this.title='AnimalTitle';
this.showName=function(){
alert(this.name);
}
}
function Cat(){
this.name='Cat';
this.showName=function(){
alert('cat'+this.name);
}
}
var animal=new Animal();
var cat=new Cat();
animal.showName.call(cat);
cat.title='catTitle';
console.log(cat.title);
call() 和 apply()的区别:

call方法调用时,必须在括号中详细地列出每个参数,参数可以是任意类型(参数不限数量)。 apply方法调用时,可在括号中以arguments 或 数组 来代表所有参数(参数只能有两个)。

它们各自的定义:

    apply:应用某一对象的一个方法,用另一个对象替换当前对象。例如:B.apply(A, arguments);即A对象应用B对象的方法。

    call:调用一个对象的一个方法,以另一个对象替换当前对象。例如:B.call(A, args1,args2);即A对象调用B对象的方法。

  它们的共同之处:

    都“可以用来代替另一个对象调用一个方法,将一个函数的对象上下文从初始的上下文改变为由thisObj指定的新对象”。

  它们的不同之处:

    apply:最多只能有两个参数——新this对象和一个数组argArray。如果给该方法传递多个参数,则把参数都写进这个数组里面,当然,即使只有一个参数,也要写进数组里。如果argArray不是一个有效的数组或arguments对象,那么将导致一个TypeError。如果没有提供argArray和thisObj任何一个参数,那么Global对象将被用作thisObj,并且无法被传递任何参数。

    call:它可以接受多个参数,第一个参数与apply一样,后面则是一串参数列表。这个方法主要用在js对象各方法相互调用的时候,使当前this实例指针保持一致,或者在特殊情况下需要改变this指针。如果没有提供thisObj参数,那么 Global 对象被用作thisObj。

    实际上,apply和call的功能是一样的,只是传入的参数列表形式不同。

prototype.constructor:

a) Prototype:每一个函数都包含一个prototype属性,这个属性指向的是一个对象的引用;而对已每一个函数(类)的实例都会从prototype属性指向的对象上继承属性,换句话说通过同一个函数创建的所有对象都继承一个相同的对象。

b) 通过new 关键字和构造函数创建的对象的原型,就是构造函数的prototype指向的那个对象

每一个函数的Prototype属性指向的对象都包含唯一一个不可枚举属性constructor,它指向了它所在的构造函数。

arguments

在JavaScript中,arguments对象是比较特别的一个对象,实际上是当前函数的一个内置属性。arguments非常类似Array,但实际上又不是一个Array实例。

innerHTML

用来获取对象的内容(还可以向对象插入内容) 这里输出的是Firstname、Lastname。

<script type="text/javascript">
function getInnerHTML(){
alert(document.getElementById("tr1").innerHTML);
}
</script>
<table border="1" id="t1">
<tr id="tr1">
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr id="tr2">
<td>Peter</td>
<td>Griffin</td>
</tr>
</table>
<br />
<input type="button" onclick="getInnerHTML()"
value="Alert innerHTML of table row" />
遍历对象的属性
function Person(name,age){
this.name=name;
this.age=age;
this.info=function(){
alert('info method');
}
}
var p=new Person('chen',22);
for(a in p){
document.writeln(p[a]);
}
prototype
function Person(name,age){
this.name=name;
this.age=age;
this.info=function(){
document.writeln(this.name);
document.writeln(this.age);
}
}
var p1=new Person('chen',22);
p1.info();
Person.prototype.walk=function(){
document.writeln(this.name+'walk...');
}
var p2=new Person('zhi',23);
p2.info();
p2.walk();// zhiwalk...
p1.walk();// chenwalk...

用这句话把walk方法嵌进去,这样Person类就有了walk方法,并且p1也能调用了。而且不污染代码。

Person.prototype.walk=function(){
document.writeln(this.name+'walk...');
}

并不是建立一个新类,是对原类进行修改。所以称为伪继承。既可以扩展自定义类,也可以扩展JS内部对象。

最新文章

  1. 图解C#的值类型,引用类型,栈,堆,ref,out
  2. 深入理解Javascript--作用域和赋值操作
  3. C#窗体 WinForm 对话框,流
  4. 论文笔记之:Progressive Neural Network Google DeepMind
  5. 第3章 K近邻法
  6. MFC中消息响应机制
  7. MyEclipse 启动tomcat时报错:Cannot change deployment state from ERROR to REDEPLOYING.ds
  8. Safari浏览器Session问题
  9. linux之SQL语句简明教程---SELECT
  10. css如何li中选中后加上class属性js控制
  11. U盘安装centos 7 提示 “Warning: /dev/root does not exist, could not boot” 解决办法
  12. Oracle压缩总结2— 估计表压缩效应
  13. Swift入门教程:基本语法(四)
  14. 大数据时代,我们为什么使用hadoop
  15. Chapter4_控制执行流程
  16. Centos7上配置网络和本地yum方法
  17. Python----Windous下安装python
  18. Centos下堡垒机Jumpserver V3.0环境部署完整记录(1)-安装篇
  19. InnoDB的锁机制浅析(All in One)
  20. 先验算法(Apriori algorithm) - 机器学习算法

热门文章

  1. python 二分法例子及冒泡排序
  2. excel 基本用法
  3. MongoDB安装及多实例启动
  4. TX2 刷机过程
  5. rsync(四)技术报告
  6. Tyvj1052(树形DP)
  7. bzoj3456
  8. TypeScript完全解读(26课时)_11.TypeScript完全解读-类型推论和兼容性
  9. POJ 1182 食物链 (破题)
  10. Qt-MVC图形视图框架初识