在其对象中调用对象的方法(Calling an object's method within its object)

编程入门 行业动态 更新时间:2024-10-24 18:24:12
其对象中调用对象的方法(Calling an object's method within its object)

我有这样的代码,我使用函数在对象中创建对象。 如您所见,我无法访问createObj,因为它尚未定义。 使用“this”并没有帮助,因为它仍然是“窗口”。 我知道一种替代方法是在创建对象o之后使用New或创建“obj1”属性,但有没有其他方法可以做到这一点?

o = { createObj: function(){ //obj creation code }, obj1: createObj() }

I have my code like this where I am creating objects within an object using a function. As you can see I cannot access createObj as it is not yet defined. Using "this" did not help because the it will still be "window". I know one alternative is to use the New or create "obj1" property after creating object o, but is there any other way to do this?

o = { createObj: function(){ //obj creation code }, obj1: createObj() }

最满意答案

你可以;

var o = (function() { function createObj() { //obj creation code } return { createObj: createObj, obj1: createObj() } })();

You could;

var o = (function() { function createObj() { //obj creation code } return { createObj: createObj, obj1: createObj() } })();

更多推荐

本文发布于:2023-07-15 19:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117741.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:其对   象中   对象   方法   method

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!