簡體   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