java认证
考试大JAVA站有什么?
关注:41134  答案:1  悬赏:60
离问题结束还有
  • 待解决
  • 2017-09-30 08:38
最佳答案
暂无最佳答案    我来回答
全部回答
  • 1楼
  • 2017-09-30 10:16
1.第一种方式,冒充对象的方式.(利用js里的每一个方法名都是一个Function对象) Java代码 function Parent(username){ this.username = username; this.say = function(){ alert(this.username); } } function Child(username,password){ this.temp = Parent;//temp指向Parent所指向的地方 。 利用js里的每一个方法名都是一个Function对象,指向一个方法。 this.temp(username);//初始化方法里的内容 delete this.temp;//temp没有用了。可以直接删除掉.this不可以丢了 //Parent(username);//这样写表面看起来是正确的,其实是错误的。因为只有new出来的对象才有this,所以调用Parent里的this就没有值了 this.password = password; this.hello = function(){ alert(this.password); } } var parent = new Parent("zhangsan"); parent.say();//zhangsan var child = new Child("lisi","123456"); child.say();//lisi child.hello();//123456 //第一种方式,冒充对象的方式.(利用js里的每一个方法名都是一个Function对象) function Parent(username){ this.username = username; this.say = function(){ alert(this.username); } } function Child(username,password){ this.temp = Parent;//temp指向Parent所指向的地方 。 利用js里的每一个方法名都是一个Function对象,指向一个方法。 this.temp(username);//初始化方法里的内容 delete this.temp;//temp没有用了。可以直接删除掉.this不可以丢了 //Parent(username);//这样写表面看起来是正确的,其实是错误的。因为只有new出来的对象才有this,所以调用Parent里的this就没有值了 this.password = password; this.hello = function(){ alert(this.password); } } var parent = new Parent("zhangsan"); parent.say();//zhangsan var child = new Child("lisi","123456"); child.say();//lisi child.hello();//123456