object-properties

PHP: change order of object properties

点点圈 提交于 2021-02-10 06:09:39
问题 Regarding PHP, is there a way to change the order of object properties? class o {public $a = 1, $b = 2;} $o = new o; foreach (get_object_vars($o) as $k => $v) { print $k . '->' . $v . PHP_EOL; } Output: a->1 b->2 Existing public variables can be unset() , and added e.g. with $o->c = 3; . But array functions do not work with objects, and I do not want to convert the object to some stdClass. The only practical workaround I can think of is to decorate an array object and overload the magic __get

Override Object Property for unit testing purposes

生来就可爱ヽ(ⅴ<●) 提交于 2020-08-10 22:54:17
问题 I am using Jest to perform unit tests on a Node.js app, where the code source is written in TypeScript and then compiled into JavaScript. In one of the classes that I wish to test, an external module is imported and a method from this module is used. I want to mock the calls to this method, in order to test only my code. However, when I run the test, I get the following error: TypeError: Cannot redefine property: methodName The problem is that this method has the following Object Properties:

Override Object Property for unit testing purposes

我的梦境 提交于 2020-08-10 22:54:14
问题 I am using Jest to perform unit tests on a Node.js app, where the code source is written in TypeScript and then compiled into JavaScript. In one of the classes that I wish to test, an external module is imported and a method from this module is used. I want to mock the calls to this method, in order to test only my code. However, when I run the test, I get the following error: TypeError: Cannot redefine property: methodName The problem is that this method has the following Object Properties:

Merge specific prop of objects into one using es6?

十年热恋 提交于 2020-01-05 04:25:11
问题 This is by far the most common question asked in SO, however, all in all, the questions asked refers to merging two whole objects. In my case, it's quite different. Suppose I'm given: const P1 = { "name" : "Person1", "profession" : "Student", "gender" : "Male", "type" : "Patient", } const E1 = { "age" : "30", "dob" : "20/12/1970", "address" : "122 Harrow Street", "contactNumber" : "07473033312", } and I want to merge these two to give me the following: const Result = { "name" : "Person1",

How to iterate through all properties and its values in Typescript class

纵然是瞬间 提交于 2019-12-23 16:54:50
问题 How do I iterate through list of class properties and get the values of each (only the properties and not the functions) class Person{ name:string; age:number; address:Address; getObjectProperties(){ let json = {}; // I need to get the name, age and address in this JSON and return it // how to do this dynamically, rather than getting one by one // like json["name"] = this.name; return json; } } Please help. 回答1: You can't do that, if you look at the compiled code of: class Person { name:

Object properties dynamic delete

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 08:33:24
问题 I am curious about an improved way to dynamically delete properties from a javascript object based on wildcards. Firstly, suppose I have the following object: object = { checkbox_description_1 : 'Chatoyant', checkbox_description_2 : 'Desultory', random_property : 'Firefly is a great program', checkbox_mood_1 : 'Efflorescent', checkbox_description_3 : 'Ephemeral' } Task Now, the end result is to have removed all properties under the guise of 'checkbox_description' and leave the rest of the

Cannot set the property value for JMS_IBM_MQMD_MsgId in weblogic, JMS, java

那年仲夏 提交于 2019-12-13 16:19:10
问题 I want to send a message to a JMS Queue, and I want to set an object property: tMessage.setObjectProperty("JMS_IBM_MQMD_MsgId", bytes); //bytes is a byte array value But I am getting an exception for this row: tMessage.setObjectProperty("JMS_IBM_MQMD_MsgId", toByteArray((phone+"IBM").toCharArray())); Why cannot I set byte array to this property? I saw some example, and everyone sets bytearray, but I am getting exception: weblogic.jms.common.MessageFormatException: [JMSClientExceptions:055123

Accessing different properties in a typescript union type

风格不统一 提交于 2019-12-11 01:44:04
问题 I'm creating a function that handles objects from the database. I have two different data structures where the same property has a different name. I can't change that, so I have to handle it in JavaScript. The objects have other differences, but that's not important to this function. I want to use the same function for two different types of objects. Here's sample code demonstrating my problem: interface TypeA { itemName: string; } interface TypeB { itemTitle: string; } function getItemName

Why is this object property undefined?

狂风中的少年 提交于 2019-12-05 18:04:05
问题 Consider the code below. The first console.log correctly logs the image, and you can see its properties in the image below. However, when I try logging one if its properties to the console, I get undefined ! console.log(that.data[0].cards); //works -- see image below console.log(that.data[0].cards.E); //undefined console.log(that.data[0].cards['E']); //undefined console.log(that.data[0].cards.hasOwnProperty('E')); //false var test = JSON.stringify(that.data[0]); console.log(test); // {} for(

Access static object property through variable name

こ雲淡風輕ζ 提交于 2019-12-04 04:11:36
问题 I know its possible to access an object property/method using a variable as its name ex.: $propName = 'something'; $something = $object->$propName; Is it possible to do the same w/ constants or static properties? I've tried: $constName = 'MY_CONST'; MyCLass::{$constName}; and $obj::{$constName}; But nothing seems to work and I couldn't find it anywhere. 回答1: Use: Class::$$constName , this is similar to normal variable variables. Demo: <?php class MyClass { public static $var = 'A'; } $name =