Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript (ECMA-6) class magic method __call like PHP

This is my use case

getSomeFields(persons, fields){

    let personsWithSpecificFields = [];

    _.each(persons, (person) => {

        let personSpecificFields = {};

        _.each(fields, (field) => {
            // here im thinking to modify the field to match the method name 
            // ( if something like __call as in php is available)
            // e.g. field is first_name and i want to change it to getFirstName
            personSpecificFields[field] = person[field]();

        });

        personsWithSpecificFields.push(personSpecificFields);
    });

    return personsWithSpecificFields;
}

Here is my person class

import _ from 'lodash';

export default class Person{

    // not working..
    __noSuchMethod__(funcName, args){
        alert(funcName);
    }

    constructor( data ){
        this.init(data);
    }

    init(data) {
        _.each(data, (value, key) => {
           this[key] = value;
        });
    }
}

I have gone through Monitor All JavaScript Object Properties (magic getters and setters), tried to implement this JavaScript getter for all properties but no luck.

I know i can do this by just writing another method which will transform my first_name to getFirstName and give it a shot. But is there any way to do this in ECMA-6 way like in class.

thanks.

like image 430
rummykhan Avatar asked Feb 15 '17 08:02

rummykhan


People also ask

What is __ call () in PHP?

When you call a method on an object of the Str class and that method doesn't exist e.g., length() , PHP will invoke the __call() method. The __call() method will raise a BadMethodCallException if the method is not supported. Otherwise, it'll add the string to the argument list before calling the corresponding function.

What is magic method in PHP?

Magic methods are special methods which override PHP's default's action when certain actions are performed on an object. Caution. All methods names starting with __ are reserved by PHP. Therefore, it is not recommended to use such method names unless overriding PHP's behavior.

What is js magic?

Overview. The Magic SDK for client-side JavaScript is your entry-point to secure, passwordless authentication for your web-based app. This guide will cover some important topics for getting started with client-side APIs and to make the most of Magic's features.


Video Answer


1 Answers

You can use a proxy to detect access to a property that your object does not have, and deal with it -- this comes close to PHP's __call:

var person = new Person();
// Wrap the object in a proxy
var person = new Proxy(person, {
    get: function(person, field) {
        if (field in person) return person[field]; // normal case
        console.log("Access to non-existent property '" + field + "'");
        // Check some particular cases:
        if (field == 'first_name') return person.getFirstName;
        // ...
        // Or other cases:
        return function () {
            // This function will be executed when property is accessed as a function
        }
    }
});

You could even do this in the constructor of your class:

class Person {
    constructor(data) {
        this.init(data);
        return new Proxy(this, {
            get: function(person, field) {
                if (field in person) return person[field]; // normal case
                console.log("Access to non-existent property '" + field + "'");
                // Check some particular cases:
                if (field == 'first_name') return person.getFirstName;
                // ...
                // Or other cases:
                return function () {
                    // This function will be executed when property is accessed as a function
                    return 15; // example
                }
            }
        });
    }
    // other methods ...
    //
}

The nice thing about proxies is that the returned object is still considered an instance of the original class. With the above code, the following will be true:

new Person() instanceof Person
like image 163
trincot Avatar answered Oct 14 '22 04:10

trincot