繁体   English   中英

如何访问不同对象的属性?

[英]How do I access properties from different objects?

我是对象和OOP的新手,我想知道如何获取对象名称以及是否有另一种访问对象属性的方法,除了下面介绍的方法。

好的,让我们举个例子:

function giveMeSomeName() {
    console.log(this)
};

giveMeSomeName();

控制台将返回该对象指向的对象,在这种情况下为“窗口”

var myObject = {
    giveMeSomeName: function() {
        console.log(this)
    }
}

控制台将返回该对象指向的对象,在这种情况下为“对象”

  1. 在这种情况下,引用对象将是“ myObject”。 假设我没有此信息,而我所拥有的只是控制台告诉我, 指向一个对象。 有没有办法得到确切的名称(在这种情况下我会寻找“myObject的”),其中所指向的对象?

让我们继续我的第二个问题,并将第二个代码片段扩展一行(this.age =“ 20”):

var myObject = {
    giveMeSomeName: function() {
        console.log(this)
        this.age = "20"
    }
}

如果要在另一个上下文中访问或操作“年龄”,在这种情况下,我将使用“ myObject.age + = 1”,它将对象“ myObject”的属性“ age”更改为21。

  1. 就语法而言,还有另一种方式可以访问“年龄”吗? this.age无法正常工作,因为它指向“ Window”而不是“ myObject”(其中“ age”未定义)。 您可能已经意识到,这个问题与第一个问题有关。

具体来说

我有以下代码。 如何在函数“ enemyMovement()”中访问“ enemy”(以下代码的下一行)?

var game = new Phaser.Game(500, 200, Phaser.Auto, '', 
    {
        preload: preload,
        create: create,
        update: update
    }
);

function preload() {
    game.load.image('tank', 'assets/tank.png')
    game.load.image('tankEnemy', 'assets/tankEnemy.png')
}

function create() {
    game.stage.backgroundColor = '#3598db'
    game.physics.startSystem(Phaser.Physics.ARCADE);
    game.world.enableBody = true;

    this.cursor = game.input.keyboard.createCursorKeys();
    this.tank = game.add.image(200, 75, 'tank')
    this.enemy = game.add.image(0, 0, 'tankEnemy')
}

function update() {
    if (this.cursor.left.isDown) {
        this.tank.position.x -= 3;
    }
    else if (this.cursor.right.isDown) {
        this.tank.position.x += 3;
    }
    else if (this.cursor.up.isDown) {
        this.tank.position.y -= 3;
    }
    else if (this.cursor.down.isDown) {
        this.tank.position.y += 3;
    }

    enemyMovement();
}

function enemyMovement() {
    enemy.position.x += 3;  //how can I access 'enemy' from above?
}

谢谢 :)

使用函数作为对象的基础时,需要将函数用作“构造函数”,这意味着您可以使用new关键字实例化它。 完成此操作后,在函数中使用this关键字会使单词绑定到实例化期间创建的对象变量。 这是创建实例属性的方式。 所以:

 // This funciton is intended to be used to construct distinct instances of objects // Notice that the name is written in Pascal Case to alert others of this fact. function GiveMeSomeName(input) { this.myProp = input; console.log(this.myProp) }; // When using a constructor function, use the `new` keyword to generate the instance // and capture the resulting object in a variable to keep each instance separate from // the next. var myObjectInstance1 = new GiveMeSomeName("foo"); var myObjectInstance2 = new GiveMeSomeName("foo2"); 

将创建对象的两个单独的实例,每个实例具有存储在其自己的instance属性中的不同数据。

同样,按照约定,应使用Pascal Case(以大写字母开头)来命名要构造为函数的函数,以使其他人知道应使用new来调用它,并且它将返回一个实例。

对于您的特定游戏代码,应该封装自己的数据和行为的游戏的每个元素都应该是一个对象,因此此代码:

function create() {
    game.stage.backgroundColor = '#3598db'
    game.physics.startSystem(Phaser.Physics.ARCADE);
    game.world.enableBody = true;

    this.cursor = game.input.keyboard.createCursorKeys();
    this.tank = game.add.image(200, 75, 'tank')
    this.enemy = game.add.image(0, 0, 'tankEnemy')
}

应该是构造函数(并用new调用),或者它本身应该创建一个新对象并返回该对象(就像工厂函数一样)。 这是一个例子:

// This will be a factory function that creates and returns an object instance
function createEnemy() {
    game.stage.backgroundColor = '#3598db'
    game.physics.startSystem(Phaser.Physics.ARCADE);
    game.world.enableBody = true;

    function Enemy(){
      this.cursor = game.input.keyboard.createCursorKeys();
      this.tank = game.add.image(200, 75, 'tank')
      this.enemy = game.add.image(0, 0, 'tankEnemy')
    }

    // Create the instance and return it:
    return new Enemy();
}

然后,您只需获取敌人的对象并像这样使用它:

// Notice here we are NOT using the `new` keyword because the 
// factory function is already doing that internally. We are just
// "catching" the resulting object that is returned from the factory.
var enemy1 = createEnemy();
enemy1.tank = ...

最后,由于所有这些取决于开发人员,记住了使用new关键字,因此JavaScript现在包含object.create()方法,该方法允许您传递将用作原型对象的对象,并返回一个新实例供您使用。

一般而言,我建议您对“ 原型”有更深入的了解。

在您的特定情况下,请尝试使用“ enemyMovement”功能扩展“游戏”对象:

game.enemyMovement = function() {
    this.enemy.position.x += 3;
}

并更改“更新”功能:

    ...
    this.enemyMovement();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM