0

Hello friends please how can i know if the className name contains a property since i'm using Ajax and i receive my class name as data.className

This is my code :

$.post(action, object, function(data){

    if(data.className.prototype.hasOwnProperty(data.methodName))
        console.log("the property exists")

    return data;
}, 'json');

My object :

{"className":"Quote", "methodName":"deleteQuote"}

This is my class Quote

class Quote{

    deleteQuote(callback){
        console.log("hello world");
    }
}

Thank you

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Here is a possible solution that does not involve eval. You would put the classes into a map, and use the map to refer to the objects by name. https://yaoganglian.com/2013/07/17/javascript-class-from-string/ – jwatts1980 May 29 '19 at 20:50

1 Answers1

0

The problem with your code is in this part: data.className.prototype. Since data.className == "Quote", this is equivalent to "Quote".prototype, which will get the prototype of the String class and not of the Quote class. So you want to convert the string "Quote" to the class which happens to be named Quote. Accessing variables named by a string is possible in Javascript as can be found here. Unfortunately, this does not work for classes and the only fix seems to be using eval. The resulting code would look like this:

var proto = eval(data.className+".prototype");
if(proto.hasOwnProperty(data.methodName))
    console.log("the property exists")

You should be careful when using eval because it might cause security issues. eval executes any code that is in the string that is passed as an argument. If you know for sure that data.className is a valid identifier, then this is not an issue. If you do not, you should first check that data.className is a valid JavaScript identifier. For how to do this, you can look at the answers to this question.

Max Meijer
  • 1,530
  • 1
  • 14
  • 23
  • TypeError: window[data.className] is undefined –  May 29 '19 at 20:31
  • @AmineBouhaddi Yes, unfortunately the solution I proposed did not actually work. I edited my answer to reflect that and added some code that should work – Max Meijer May 29 '19 at 20:41
  • Firstly i thank you so much for your effort and help, and i want just to know if eval is secure ? –  May 29 '19 at 20:45
  • It is mostly not: https://security.stackexchange.com/questions/94017/what-are-the-security-issues-with-eval-in-javascript – jwatts1980 May 29 '19 at 20:47
  • @AmineBouhaddi I explained the security problems of using eval further – Max Meijer May 29 '19 at 20:56
  • @MaxMeijer thank you can you help me more to check my identifier –  May 29 '19 at 20:58
  • @MaxMeijer i thank you so much, i have just one more question please how can i call the methodName if its exists –  May 29 '19 at 21:05
  • @AmineBouhaddi I recommend that you ask the security thing in a new question if you need more help with that. You have the code `var proto = eval(data.className+".prototype");` and want to check that `data.className` is a valid javascript identifier to avoid XSS – Max Meijer May 29 '19 at 21:07
  • @AmineBouhaddi You can use `proto[data.methodName]()` – Max Meijer May 29 '19 at 21:08
  • @MaxMeijer i thank you so much, i will create a new function about the security –  May 29 '19 at 21:14
  • @MaxMeijer how can i create the object also please :) –  May 30 '19 at 02:29
  • @MaxMeijer can you check my question her please ? https://stackoverflow.com/questions/56370348/how-can-i-call-a-function-inside-another-one-in-javascript?noredirect=1#comment99343159_56370348 –  May 30 '19 at 02:30