I do have this kind problem below that create a new constructor and must return a string in an object. My data is an object array like this
val _List = [{name: 'name1', number: 2, 'message': 'Hello World'},{name: 'name1', number: 2, 'message': 'Good bye'}]
And the code will be executed by a test
test2 = new ProgramDevice('2');
it('should send string exactly', function() {
expect(test2.postStringReturn).toEqual('Hello world!');
}
The solution that I created is something like below but instead of string return it returns a function
this.postStringReturn= _List.find(data => {
if(data.number == this.number ) {
return data.message
}
});
Can someone explain where I did wrong? Sorry not really good at OOP, just someone tell me where is my mistake
Update //
If I create something like this
var ProgramDevice = function (num) { // i.e. '192.168.0.1'
this.number= num;
}
ProgramDevice.prototype = {
postStringReturn = _List.find(data => { data.number == this.number }).message
}
How can I get the this.number in using prototype
this.postStringReturn= _List.find(data => data.number == this.number).message;