Dynamically reference class properties
Date : March 29 2020, 07:55 AM
I wish this help you , Do you mean this? sumobj[prop1]
|
Compare/access object's properties dynamically with string reference
Tag : chash , By : Paul McKee
Date : March 29 2020, 07:55 AM
Any of those help I have a simple class: , You'll need to use the GetValue method instead list.Where(o => o.GetType().GetProperty("Source").GetValue(o, null) == "Me").Single()
|
Accessing dynamically incremented variable JS
Date : March 29 2020, 07:55 AM
wish helps you You could pass a callback function to time() You can not return inside the setTimeout callback as there is nothing to return to. let daycount = 0,
day = $('#day');
function time(callback) {
setInterval(function() {
daycount++;
day.html('Day ' + daycount);
callback(daycount);
}, 1000);
}
function new_game() {
function checkCount(daycount) {
if (daycount == 5) {
console.log('Is 5');
} else {
console.log('Not 5');
}
}
time(checkCount);
}
new_game()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="day">
|
Is there a way that I can dynamically pass object names to a function (as arguments) and reference properties of that ob
Date : March 29 2020, 07:55 AM
I wish this help you Here is an answer to this: 'How do I convert a string to a variable name?' First, you need a surrounding object. You can use the Window object, but it is not recommended. So, you can see here that I created a simple class that contains two properties that represent your objects "sullivan" is the player and "johnson" is the computer. class Controller {
constructor() {
this.newAttack = 0;
this.defeatedEnemies = 0;
this.sullivan = {
name: "Sullivan",
attack: 4,
health: 10
};
this.johnson = {
name: "Johnson",
counterAttack: 8,
health: 10
};
}
battleFunction(player, computer) {
//increments playerAttack
this.newAttack += this[player].attack;
this[player].health -= this[computer].counterAttack;
//deducts attack from computer HP
if (this.newAttack > this[player].attack) {
this[computer].health -= this.newAttack;
console.log(this[computer].health);
} else {
this[computer].health -= this[player].attack;
console.log(this[computer].health);
}
if (this[computer].health <= 0) {
this.defeatedEnemies++
}
if (this.defeatedEnemies >= 4) {
alert("You won!");
//resetGame();
}
};
}
const ctrl = new Controller();
$("#fightBtn").click(() => {
ctrl.battleFunction($("#playerDiv").children("img").attr("id"), $("#computerDiv").children("img").attr("id"));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="fightBtn">Fight!</button>
<div id="playerDiv"><img id="sullivan"></div>
<div id="computerDiv"><img id="johnson"></div>
|
Dynamically set incremented link id using jquery?
Date : March 29 2020, 07:55 AM
|