JavaScript:基于现有对象创建自定义类的新对象(向其添加方法)

JavaScript:基于现有对象创建自定义类的新对象(向其添加方法),javascript,object,Javascript,Object,我用这个来确定岩石 每个使用这个框架的人都会告诉你它很棒,但是它缺少很多结构 因此,我必须处理的实例,代表我的应用程序的不同屏幕。为了更加面向对象,我希望每个屏幕都有一个特定的类,这样我就可以添加特定的方法,比如 myScreen1.tapDoneButton(); var total = myScreen2.getNumberOfElements(); 目前,我可以通过将UIAWindow的实例传递给将添加适当方法的函数来实现这一点,如下所示: function makeMainScreen

我用这个来确定岩石

每个使用这个框架的人都会告诉你它很棒,但是它缺少很多结构

因此,我必须处理的实例,代表我的应用程序的不同屏幕。为了更加面向对象,我希望每个屏幕都有一个特定的类,这样我就可以添加特定的方法,比如

myScreen1.tapDoneButton();
var total = myScreen2.getNumberOfElements();
目前,我可以通过将UIAWindow的实例传递给将添加适当方法的函数来实现这一点,如下所示:

function makeMainScreen(actualScreen)
{   
    actualScreen.constructor.prototype.getAddButton = function() {
        return this.buttons()["add button"];
    };
    actualScreen.constructor.prototype.tapAddButton = function() {
        this.getAddButton().tap();
    };
    // Add any desired method...

return actualScreen;
}
var mainScreen = makeMainScreen(app.mainWindow());
mainScreen.tapAddButton();
function MainScreen(actualScreen){
  // This line doesn't work : because 'this' is immutable
  this = actualScreen;

  this.tapAddButton = function(){
    this.getAddButton().tap();
  }

  //...

}
var mainScreen = new MainScreen(app.mainWindow());
mainScreen.tapAddButton();
它很好用,我是这样用的:

function makeMainScreen(actualScreen)
{   
    actualScreen.constructor.prototype.getAddButton = function() {
        return this.buttons()["add button"];
    };
    actualScreen.constructor.prototype.tapAddButton = function() {
        this.getAddButton().tap();
    };
    // Add any desired method...

return actualScreen;
}
var mainScreen = makeMainScreen(app.mainWindow());
mainScreen.tapAddButton();
function MainScreen(actualScreen){
  // This line doesn't work : because 'this' is immutable
  this = actualScreen;

  this.tapAddButton = function(){
    this.getAddButton().tap();
  }

  //...

}
var mainScreen = new MainScreen(app.mainWindow());
mainScreen.tapAddButton();
但这似乎还不够面向对象,我想创建真正的对象,使用newthis关键字,所以我有一个这样的声明:

function makeMainScreen(actualScreen)
{   
    actualScreen.constructor.prototype.getAddButton = function() {
        return this.buttons()["add button"];
    };
    actualScreen.constructor.prototype.tapAddButton = function() {
        this.getAddButton().tap();
    };
    // Add any desired method...

return actualScreen;
}
var mainScreen = makeMainScreen(app.mainWindow());
mainScreen.tapAddButton();
function MainScreen(actualScreen){
  // This line doesn't work : because 'this' is immutable
  this = actualScreen;

  this.tapAddButton = function(){
    this.getAddButton().tap();
  }

  //...

}
var mainScreen = new MainScreen(app.mainWindow());
mainScreen.tapAddButton();
我会这样使用它:

function makeMainScreen(actualScreen)
{   
    actualScreen.constructor.prototype.getAddButton = function() {
        return this.buttons()["add button"];
    };
    actualScreen.constructor.prototype.tapAddButton = function() {
        this.getAddButton().tap();
    };
    // Add any desired method...

return actualScreen;
}
var mainScreen = makeMainScreen(app.mainWindow());
mainScreen.tapAddButton();
function MainScreen(actualScreen){
  // This line doesn't work : because 'this' is immutable
  this = actualScreen;

  this.tapAddButton = function(){
    this.getAddButton().tap();
  }

  //...

}
var mainScreen = new MainScreen(app.mainWindow());
mainScreen.tapAddButton();
我认为我可以将实际屏幕保存为对象的属性(如下面Grace Shao的回答),并调用其上的所有方法,但我希望保留原始的UIAWindow方法

有人知道怎么做吗?
或者,也许我试图实现的目标没有意义,在这种情况下,我很乐意知道。

如果我理解正确,您可以尝试以下方法:

function MainScreen(actualScreen){
    this.screen = actualScreen;
}

MainScreen.prototype.tapAddButton = function () {
    this.screen.getAddButton().tap();
};

MainScreen.prototype.getScreen = function () {
    return this.screen;
};

//...

var mainScreen = new MainScreen(app.mainWindow());

mainScreen.tapAddButton();
您不能将任何内容分配给此,这是正确的。您还可以在构造函数
MainScreen
中定义方法,但它们将被视为特权成员

function MainScreen(actualScreen){
    this.screen = actualScreen;

    this.tapAddButton = function () {
        this.screen.getAddButton().tap();
    };
}
如果不希望它们成为特权成员,最好在构造函数之外定义它们。否则,每次实例化新对象时,成员都会被反复初始化

已更新: 您还可以在构造函数中包装
screen
方法,如下所示

var prop;

for (prop in actualScreen) {
    if (typeof actualScreen[prop] !== 'Function') {
        continue;
    }
    this[prop] = function () {
        return actualScreen[prop].apply(actualScreen, arguments);
    };
}

如果我理解正确,您可以尝试以下方法:

function MainScreen(actualScreen){
    this.screen = actualScreen;
}

MainScreen.prototype.tapAddButton = function () {
    this.screen.getAddButton().tap();
};

MainScreen.prototype.getScreen = function () {
    return this.screen;
};

//...

var mainScreen = new MainScreen(app.mainWindow());

mainScreen.tapAddButton();
您不能将任何内容分配给此,这是正确的。您还可以在构造函数
MainScreen
中定义方法,但它们将被视为特权成员

function MainScreen(actualScreen){
    this.screen = actualScreen;

    this.tapAddButton = function () {
        this.screen.getAddButton().tap();
    };
}
如果不希望它们成为特权成员,最好在构造函数之外定义它们。否则,每次实例化新对象时,成员都会被反复初始化

已更新: 您还可以在构造函数中包装
screen
方法,如下所示

var prop;

for (prop in actualScreen) {
    if (typeof actualScreen[prop] !== 'Function') {
        continue;
    }
    this[prop] = function () {
        return actualScreen[prop].apply(actualScreen, arguments);
    };
}

谢谢你的快速回答。我考虑过这个选项,但若我想调用UIAWindow的现有方法,我应该调用mainScreen.screen.theExistingMethod();我想调用mainScreen.theExistingMethod()@朱利安:好问题!我刚刚更新了我的答案。希望这是你要找的东西。这是个好把戏。我认为会有一个干净的解决方案(比如定义一个类“扩展”另一个类),但显然不是。我会等一会儿,如果没有更好的答案,我会接受你的。谢谢谢谢你的快速回答。我考虑过这个选项,但若我想调用UIAWindow的现有方法,我应该调用mainScreen.screen.theExistingMethod();我想调用mainScreen.theExistingMethod()@朱利安:好问题!我刚刚更新了我的答案。希望这是你要找的东西。这是个好把戏。我认为会有一个干净的解决方案(比如定义一个类“扩展”另一个类),但显然不是。我会等一会儿,如果没有更好的答案,我会接受你的。谢谢我开始回答,但我认为您已经在那里了-只需使用现有的函数,调用它
MainScreen
,然后使用
new
调用它。在本例中,这与Javascript一样是面向对象的。尝试做得更多只会增加不必要的复杂性。我开始回答,但我认为您已经做到了-只需使用现有功能,但调用它
MainScreen
,并使用
new
调用它。在本例中,这与Javascript一样是面向对象的。试图做得更多只会增加不必要的复杂性。