首页 新闻 赞助 找找看

Javascript代码的小小疑问(11)

0
[已解决问题] 解决于 2015-06-05 12:33

一.先看以下一段js代码:

var parentFunc = function(pf) {

   parentFunc.prototype.pf = 1;

      return function() {

           alert('parentFunc' + pf)

                 }

             };

var childFunc = function(){

  parentFunc.call(this,arguments);

     childFunc.prototype.cf = 2;  

       return function(){

    alert('childFunc ')

              }

          };

childFunc ()();   //弹出“childFunc ”

二.问题

我想用childFunc ()();这种形式,弹出 parentFunc里的pf值和弹出信息,这里的调用该怎么写?

Coca-code的主页 Coca-code | 初学一级 | 园豆:10
提问于:2015-06-05 11:08
< >
分享
最佳答案
0

var parentFunc = function(){

  var pf=1;

  alert('parentFunc' + pf);

}

var childFunc = function(){

  return parentFunc;

}

childFunc()();

奖励园豆:5
xmodygetz | 小虾三级 |园豆:575 | 2015-06-05 11:20

你代码是行是行,

但我在运用继承,call(),

我想子继承父,子就又了父的属性和方法,

所以不能结贴给你~

Coca-code | 园豆:10 (初学一级) | 2015-06-05 11:34

@殷敏峰: 要继承写法是完全不一样的。因为继承围绕的是对象,而非函数。

xmodygetz | 园豆:575 (小虾三级) | 2015-06-05 11:46

@xmodygetz: 

function parentFunc(pf) {  

  parentFunc.prototype.pf = 1;

  return function() {

  alert('parentFunc' + pf)

    }

  };

 

function childFunc() {  

  parentFunc.call(this, arguments);

   childFunc.prototype.cf = 2;  

   return function() {    

    alert('childFunc ')

    }

  };

childFunc()();

 

嗯,就是这样了,怎么显示父的弹出信息?

Coca-code | 园豆:10 (初学一级) | 2015-06-05 11:56

@殷敏峰: 

继承
var parentFunc=function(){
}
parentFunc.prototype.pf=1;
parentFunc.prototype.alert=function(){
alert('parentFunc' + this.pf);
}

var childFunc = new parentFunc();//这是一个对象
childFunc.alert();

call//虽然感觉没意义
var parentFunc=function(){
var pf=1;
alert('parentFunc' + pf);
}
var childFunc=function(){
return function(){
parentFunc.call(this);
}
}
childFunc()();

 

最好把目的弄清楚再问。

xmodygetz | 园豆:575 (小虾三级) | 2015-06-05 12:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册