1024programmer JavaScript Using PHP-like magic methods in JS

Using PHP-like magic methods in JS

Javascript magic methods

This script uses Proxy to implement magic methods similar to PHP in Javascript.

Example

You can use it like this:

const Foo = magicMethods  (class Foo {
   constructor () {
     this.bar = 'Bar'
   }
   __get(name) {
     return `[[${name}]]`
   }
 })
 const foo = new Foo
 foo. bar // "Bar"
 foo.baz // "[[baz]]"

If you are using a Javascript compiler like Babel with decorators enabled, you can also use magicMethods functions as decorators:

@magicMethods
 class Foo {
   //...
 }

Magic methods supported

Given a class Class and instance, the following are the magic methods supported by this script:

__get (name)

Called when an attempt is made to access instance[name] and name is not an attribute of instance.

Note: In PHP, checking for the existence of name in an instance does not use any custom __isset() methods.

__set(name, value)

This method is called when an attempt is made to use instance[name] = … and the instance does not have a name attribute set.

__isset(name)

This method is called when an attempt is made to check whether name exists by calling name in instance.

__unset(name)

This method is called when trying to unset the name attribute via delete instance[name].

Other methods

The following magic methods are supported by this script, but not in PHP:

static __getStatic (name)

Similar to __get(), but it is used on Class instead of instance.

static __setStatic(name, value)

Similar to _ _set(), but used in Class instead of instance.

Why is magic method X not supported?

They are either unnecessary or impractical:

__construct() is not needed, Javascript already has a constructor.

__destruct(): There is no hook mechanism for object destruction in Javascript.

__call(): Contrary to PHP, methods are like attributes in Javascript, first obtained through __get(). To implement __call(), you simply return a function from get().

__callStatic(): Similar to __call(), but with __getStatic().

__sleep(), __wakeup(): Javascript does not have built-in serialization and deserialization. You can use JSON.stringify() and JSON.parse(), but they don’t have a mechanism to trigger any methods automatically.

__toString() has a corresponding toString() in Javascript

__invoke(): If you try to call a non-function object, Javascript will throw an error, which will not avoid.

__set_state(): There is no analogue of var_export() in Javascript.

__clone(): Hook for built-in cloning functionality in Javascript.

__debugInfo(): Unable to hook into console.log() output.

Can I extend classes with magic methods?

Yes, to an extent:

class Bar extends Foo {}
 // Or, if the class Bar itself contains magic methods:
 const Bar = magicMethods(class Bar extends Foo {
   //...
 })

But unfortunately, you cannot access properties in the parent class from the child class:

const Foo = magicMethods(  class Foo {
   __get() {
     return this. bar()
   }
 })
 class Bar extends Foo {
   bar() {
     return & # 39; value & # 39;
   }
 }
 // This *doesn't* call B's bar() method, but instead throws a TypeError:
 (new Bar).something

Recommended tutorial: “JS Tutorial”

The above is the detailed content of using PHP-like magic methods in JS. For more, please pay attention to other related articles on 1024programmer.com!

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/using-php-like-magic-methods-in-js/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索