功能“实时”组合(Functional “real-time” composition)
我最近发现了一篇很棒的文章,内容涉及对象组合VS传统继承的好处。
希望我的问题不会被标记为自以为是,但我想知道在对象基于用户的游戏交互进行更改时使用合成的好方法。
以文章代码为例:
const canCast = (state) => ({ cast: (spell) => { console.log(`${state.name} casts ${spell}!`); state.mana--; } }) const canFight = (state) => ({ fight: () => { console.log(`${state.name} slashes at the foe!`); state.stamina--; } }) const fighter = (name) => { let state = { name, health: 100, stamina: 100 } return Object.assign(state, canFight(state)); } const mage = (name) => { let state = { name, health: 100, mana: 100 } return Object.assign(state, canCast(state)); } scorcher = mage('Scorcher') scorcher.cast('fireball'); // Scorcher casts fireball! console.log(scorcher.mana) // 99 slasher = fighter('Slasher') slasher.fight(); // Slasher slashes at the foe! console.log(slasher.stamina) // 99
如何在运行时使用组合来更改Character对象的状态? 而不是已经存在的Mage对象,我希望Character对象基于游戏事件而改变,例如。 角色捡起一名工作人员,现在成为一名现在可以施法的“法师”。 我想到的第一件事就是在Character中有一个状态属性,它根据交互而变化,而Character则以某种方式“继承”现在施放并获得法术力状态属性的能力。
I recently came across a great article covering the benefits of object composition VS traditional inheritance.
Hopefully my question is not going to be flagged as opinionated but I'd like to know a good approach to using composition for when an object changes based on a user's game interaction.
Using the articles code as an example:
const canCast = (state) => ({ cast: (spell) => { console.log(`${state.name} casts ${spell}!`); state.mana--; } }) const canFight = (state) => ({ fight: () => { console.log(`${state.name} slashes at the foe!`); state.stamina--; } }) const fighter = (name) => { let state = { name, health: 100, stamina: 100 } return Object.assign(state, canFight(state)); } const mage = (name) => { let state = { name, health: 100, mana: 100 } return Object.assign(state, canCast(state)); } scorcher = mage('Scorcher') scorcher.cast('fireball'); // Scorcher casts fireball! console.log(scorcher.mana) // 99 slasher = fighter('Slasher') slasher.fight(); // Slasher slashes at the foe! console.log(slasher.stamina) // 99
How do I use composition to change the state of the Character object during run-time? Instead of the Mage object already existing I want the Character object to change based on a game event eg. Character picks up a staff and now becomes a "Mage" who can now Cast spells. First thing that comes to mind is to have a state property in Character that changes based on the interaction and the Character somehow "inherits" the ability to now Cast and gains a mana state property.
原文:https://stackoverflow.com/questions/50844719
最满意答案
尝试这个。 我必须通过转义每个的尾随引号来修复无效的JSON
\"Test Product 1" <<<< \"Test Product 2" <<<<
var data = [ { "product": "[{\"imgUrl\":\"/p/332401\",\"prodCartQty\":2,\"prodName\":\"Test Product 1\",\"productCode\":\"332401\"},{\"imgUrl\":\"/p/332388\",\"prodCartQty\":2,\"prodName\":\"Test Product 2\",\"productCode\":\"332388\"}]", "category": "gross_MAX_1", "totalCategory": 1 }, { "product": "[{\"imgUrl\":\"/p/332401\",\"prodCartQty\":2,\"prodName\":\"Test Product 1\",\"productCode\":\"332401\"},{\"imgUrl\":\"/p/332388\",\"prodCartQty\":2,\"prodName\":\"Test Product 2\",\"productCode\":\"332388\"}]", "category": "gross_MAX_1", "totalCategory": 1 } ] $.each(data,function(_,productArr) { var arr = JSON.parse(productArr.product.replace(/\//g,"")); $.each(arr,function(_,productDetails) { // here is a product detail object $.each(productDetails,function(name,value) { console.log(name,value); }); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Try this. I had to fix the invalid JSON by escaping the trailing quote on each
\"Test Product 1" <<<< \"Test Product 2" <<<<
var data = [ { "product": "[{\"imgUrl\":\"/p/332401\",\"prodCartQty\":2,\"prodName\":\"Test Product 1\",\"productCode\":\"332401\"},{\"imgUrl\":\"/p/332388\",\"prodCartQty\":2,\"prodName\":\"Test Product 2\",\"productCode\":\"332388\"}]", "category": "gross_MAX_1", "totalCategory": 1 }, { "product": "[{\"imgUrl\":\"/p/332401\",\"prodCartQty\":2,\"prodName\":\"Test Product 1\",\"productCode\":\"332401\"},{\"imgUrl\":\"/p/332388\",\"prodCartQty\":2,\"prodName\":\"Test Product 2\",\"productCode\":\"332388\"}]", "category": "gross_MAX_1", "totalCategory": 1 } ] $.each(data,function(_,productArr) { var arr = JSON.parse(productArr.product.replace(/\//g,"")); $.each(arr,function(_,productDetails) { // here is a product detail object $.each(productDetails,function(name,value) { console.log(name,value); }); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
相关问答
更多-
数组中的JSON数组(JSON array within an array)[2023-11-29]
你的问题出现在你的$.each你已经把它降低到data.results.image的水平,然后你正在寻找results.image 。 所以对于你的jQuery脚本,你正在寻找每一个data.results.image.results.image ,而这是不存在的。 要循环遍历每个results对象,您需要在$.each上执行$.each ,然后查找element.image 。 这是一个工作的JSFiddle,它演示了这个: http : //jsfiddle.net/7udv3uoh/4/ Your p ... -
只需创建一个新的JSONArray。 JSONArray otherJsonArray = new JSONArray(); 或者遍历数组并remove(int index)索引。 http://www.json.org/javadoc/org/json/JSONArray.html#remove(int) Just create a new JSONArray. JSONArray otherJsonArray = new JSONArray(); Or iterate through the arr ...
-
首先你必须解码你的json数据 $json = json_decode($data[0]['json']); 然后你可以访问你的AfterParticipationHeader $json->Canvas[0]->MainObjects->{"After Participation"}->afterParticipationHeader First you have to decode your json data $json = json_decode($data[0]['json']); Then ...
-
删除数组中的JSON数据(Delete JSON data in array)[2022-03-24]
您可以尝试使用lodash方法来实现您的目标: - var _ = require('lodash'); socket.on('disconnect', function(){ _.pullAllWith(users, [{ user:socket.nick, userid:socket.id, socket:socket }], _.isEqual) }); }) ... -
从JSON数组中获取数据(Fetching data from JSON array)[2022-11-13]
function filterOnPlayerStatSummary(myObject, filter) { var result = []; for(var i = 0; i < myObject.length; i++) { if (myObject[i].playerStatSummary == filter) { result.push(myObject[i]); } } retur ... -
JSON从数据中获取数组键(JSON get array key from data)[2022-07-23]
我假设您需要在变量中获取json key名称 尝试这个: $.each(result, function(key, value){ console.log(key, value); }) result是json结构即 result = { 'Name':'anything'}; I am assuming you need to get the json key name in a variable try this: $.each(result, function(key, value){ conso ... -
尝试这个。 我必须通过转义每个的尾随引号来修复无效的JSON \"Test Product 1" <<<< \"Test Product 2" <<<< var data = [ { "product": "[{\"imgUrl\":\"/p/332401\",\"prodCartQty\":2,\"prodName\":\"Test Product 1\",\"productCode\":\"332401\"},{\"imgUrl\":\"/p/332388\",\"prod ...
-
从外部Json获取数组(Get array from external Json)[2021-06-17]
// Define it where you want it to be accessible var people; $.getJSON("file.json", function (json) { // Store the data in people to make it accessible globally people = json.data; people.forEach(function(person){ console.log(person.Name); }); ... -
使用jQuery jQuery.getJSON() : $.getJSON('ajax/test.json', function(data) { console.log(data); //see your data ( works in Chrome / FF with firebug) console.log(data["Restoration"][0]["easy"]["value"]) //should output 1 }); using jQuery jQuery.get ...
-
试试吧 foreach($jsonarray as $k=>$alldata) { foreach($alldata["Bestaende"] as $idx => $Bestaende) { echo '
'; echo 'Print Menge: '.$Bestaende['Menge']; echo '
'; } } Try it like this foreach($jsonarray as $k=>$alldata ...