javaScript 之this的使用

1、this的使用及其代表的对象 

 1 <script text="javascript">  
 2     function Person(name,age,job){  
 3         this.name = name;  
 4         this.age = age;  
 5         this.job = job;  
 6     }  
 7     Person.sayName= function(){   
 8         alert(this.name);  
 9     };  
10     var tanya = new Person("tanya","30","female");  
11     var ansel = new Person("ansel","30","male");  
12     tanya.sayName();  
13     ansel.sayName();  
14 </script>  

在sayName方法中的this代表 Person对象。在这段代码中,this代表调用sayName方法的对象。 
2、通过this实现继承, 

 1 <script text="javascript">  
 2     function Parent(username){  
 3         this.username=username;  
 4         this.sayHello=function()  
 5         {  
 6             alert(this.username)      
 7         }  
 8     }  
 9   
10     function Child(username,password){  
11           
12         this.method=Parent;  
13         this.method(username);  
14         delete this.method;  
15         this.password=password;             
16     }  
17 </script>  

在上面的这段代码中,this.method=Parent; 把Parent 当做方法调用,thid.method(username)调用方法的时候,Parent中的this代表Child对象,这样就是实现了把Parent中的方法和属性传递给Child对象。

javaScript的继承

http://www.cnblogs.com/snandy/archive/2011/03/09/1977804.html

原文地址:https://www.cnblogs.com/laj12347/p/2962592.html