在匿名函数中,如何使用该函数的属性?

饶鹏 2010-02-28 02:52:34

CType = function(Text)
{this.nType = GetType(Text);}

CType.GetType = function(Text)
{/* ... */}

// ...

Type = new CType(Str);


好像arguments.callee.GetType可以?能不能直接写GetType?
...全文
184 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
2010-03-01
  • 打赏
  • 举报
回复
用prototype呢?


CType = function(Text)
{this.nType = this.GetType(Text);}

CType.prototype.GetType = function(Text)
{return Text + "1";}

Type = new CType("abc");
alert(Type.nType);

s_title 2010-03-01
  • 打赏
  • 举报
回复
哥们:看你都提了几个javascript 面向对象的问题了。我要告诉你的是。千万不要用别的面向对象的语言来

来理解javascript的面向对象。 比如说继承, this... 这些东西。 如果你那样做了。

马上改过来吧。 不然你无法真正理解javascript 。javascript 是非常灵活,松散的动态语言。并不像别的非动

态语言的方式来实现面向对象的编程。比如继承: 实际上javascript并不支持继承,只能说是模拟继承。

看看这个帖子吧: http://topic.csdn.net/u/20100225/11/4c2cd54f-6347-4c04-b8d3-25a4d169bb0b.html

希望对你有所帮助。
饶鹏 2010-03-01
  • 打赏
  • 举报
回复
引用 13 楼 s_title 的回复:
哥们:看你都提了几个javascript 面向对象的问题了。我要告诉你的是。千万不要用别的面向对象的语言来

来理解javascript的面向对象。 比如说继承, this... 这些东西。 如果你那样做了。

马上改过来吧。 不然你无法真正理解javascript 。javascript 是非常灵活,松散的动态语言。并不像别的非动

态语言的方式来实现面向对象的编程。比如继承: 实际上javascript并不支持继承,只能说是模拟继承。

看看这个帖子吧: http://topic.csdn.net/u/20100225/11/4c2cd54f-6347-4c04-b8d3-25a4d169bb0b.html

希望对你有所帮助。


确实,准备放弃了。模拟别人终归无法完全发挥自身的优势。
饶鹏 2010-02-28
  • 打赏
  • 举报
回复
上面那个问题弄明白了,this指代的是Type这个对象,它不是函数,所以没有prototype属性。
饶鹏 2010-02-28
  • 打赏
  • 举报
回复
<html><body><script type="text/javascript">

CType = function(Text)
{
// this.nType = CType.GetType(Text);
// this.nType = arguments.callee.GetType(Text);
//! this.nType = this.prototype.constructor.GetType(Text);
this.nType = this.constructor.GetType(Text);
//! this.nType = GetType(Text);

// this.MyGetType = function(Text) {return CType.GetType(Text);}
//! this.MyGetType = function(Text) {return arguments.callee.GetType(Text);}
//! this.MyGetType = function(Text) {return this.prototype.constructor.GetType(Text);}
this.MyGetType = function(Text) {return this.constructor.GetType(Text);}
//! this.MyGetType = function(Text) {return GetType(Text);}
}

CType.GetType = function(Text) {return Text;}

// ...

Type = new CType("OK");

document.write("Type.nType: " + Type.nType + "<br />");
document.write("Type.MyGetType(): " + Type.MyGetType("OK") + "<br />");

</script></body></html>


上面注释形式是"//!"的表示调用不成功。

令我惊奇的是,在MyGetType方法里,this.prototype为未定义(不过arguments.callee.prototype倒是存在)。
饶鹏 2010-02-28
  • 打赏
  • 举报
回复
<html>
<body>

<script type="text/javascript">

CType = function(Text)
{
this.nType0 = CType.GetType(Text);
this.nType1 = arguments.callee.GetType(Text);
this.nType2 = this.prototype.constructor.GetType(Text);
this.nType3 = GetType(Text);

this.MyGetType0 = function(Text) {return CType.GetType(Text);}
this.MyGetType1 = function(Text) {return arguments.callee.GetType(Text);}
this.MyGetType2 = function(Text) {return this.prototype.constructor.GetType(Text);}
this.MyGetType3 = function(Text) {return GetType(Text);}
}

CType.GetType = function(Text)
{return Text;}

// ...

Type = new CType("Hello");

alert("Type.nType0: " + Type.nType0);
alert("Type.nType1: " + Type.nType1);
alert("Type.nType2: " + Type.nType2);
alert("Type.nType3: " + Type.nType3);
alert("Type.MyGetType0(): " + Type.MyGetType0("World"));
alert("Type.MyGetType1(): " + Type.MyGetType1("World"));
alert("Type.MyGetType2(): " + Type.MyGetType2("World"));
alert("Type.MyGetType3(): " + Type.MyGetType3("World"));

</script>

</body>
</html>


正在挨个测试
wtcsy 2010-02-28
  • 打赏
  • 举报
回复
函数原形下的constructor引用的是函数本身

可以测试一下
CType = function(Text){
this.nType = GetType(Text);
alert(1);
alert(2)
}
function GetType(Text){/* ... */}
CType.prototype.constructor();


你这个是什么意思 看不懂
在那个匿名函数的方法里,arguments.callee应该是指代这个方法本身,而不是匿名函数

你能写个例子吗??//?
饶鹏 2010-02-28
  • 打赏
  • 举报
回复
引用 6 楼 wtcsy 的回复:
this.prototype.constructor.GetType
这么写应该不行吧
this是指向调用该函数的对象
很可能就不是指向CType 的


CType.prototype.constructor.GetType可以
但是this.prototype.constructor.GetType
是不行的吧
同你说的 CType不确定  用arguments.callee算了


这问题大了……
在那个匿名函数的方法里,arguments.callee应该是指代这个方法本身,而不是匿名函数。
这个方法的prototype.constructor指代的又是什么?
晕了……
饶鹏 2010-02-28
  • 打赏
  • 举报
回复
引用 5 楼 wtcsy 的回复:
我想问 一下
CType = function(Text)
    {this.nType = GetType(Text);}

Ctype是定义在全局里面的吧

那在这个函数的作用域下还能找到它???????


CType肯定是和函数同一个作用域的,所以函数内部能够使用CType。
但是我想随时随地更改CType这个名字,所以函数里面最好不要用它,否则一改就全都要改了。
wtcsy 2010-02-28
  • 打赏
  • 举报
回复
this.prototype.constructor.GetType
这么写应该不行吧
this是指向调用该函数的对象
很可能就不是指向CType 的


CType.prototype.constructor.GetType可以
但是this.prototype.constructor.GetType
是不行的吧
同你说的 CType不确定 用arguments.callee算了
wtcsy 2010-02-28
  • 打赏
  • 举报
回复
我想问 一下
CType = function(Text)
{this.nType = GetType(Text);}

Ctype是定义在全局里面的吧

那在这个函数的作用域下还能找到它???????
饶鹏 2010-02-28
  • 打赏
  • 举报
回复
To TinyJimmy:

我事先并不能确定这个函数叫CType,所以不能这么用。



另外追加一点,如果在这个函数的方法中,又如何使用这个函数的属性?
this.prototype.constructor.GetType?
TinyJimmy 2010-02-28
  • 打赏
  • 举报
回复

CType = function(Text)
{this.nType = CType.GetType(Text);}

CType.GetType = function(Text)
{/* ... */}

// ...

Type = new CType(Str);
饶鹏 2010-02-28
  • 打赏
  • 举报
回复
不是说优先寻找内部作用域里的名字么?
我在函数内部直接使用函数的属性名,应该合法吧?
wtcsy 2010-02-28
  • 打赏
  • 举报
回复
arguments.callee.GetType是可以
直接写不可以吧
总得找一个对这个函数的引用吧

87,997

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧