Javascript magic methods

In php, at least in my example, magic methods are very often used - at least when defining the main classes from which it will be distributed the most.

The magic methods in php act in a special way from the norm of your usual methods. For example, one of the most common methods in my book for the game will be __construct ()

The construction is performed every time a class is loaded. For example, if you want your class to introduce itself, you can do something like this:

<?php class Person { function __construct($name) { $this->name = $name; $this->introduceYourself(); } public function introduceYourself() { //if $this->name then echo $this->name else echo i dont have name echo $this->name ? "hi my name is " . $this->name : "error i dont have name"; } } $dave = new Person('dave'); 

Usually you did not pass anything into the construct.

Some others that I come across usually include:

__ call (), which allows you to change the default method called by methods. A good example is redefinition, which allows you to get attribute values ​​whenever you use a method that starts with the word get, or by setting attribute values ​​whenever a method call starts with a set of words.

__ get () is used as an additional loader for class attributes, I do not use, but someone may be interested.

__ set (), used as a reloader for class attributes, I do not use, but someone might be interested.

__ destruct () I also do not use, is called as soon as there are no other references to a specific object or in any order during the shutdown sequence.

Question

Are there any such magic methods inside javascript?

Are there any hidden stones that the new javascript programmer should know, like the ones I described above for php?

+6
source share
3 answers

If by "magic" you mean methods that are called implicitly, Javascript has toString and valueOf :

 > foo = {toString: function() { return 'hi' }} > foo + " there" "hi there" > foo = {valueOf: function() { return 100 }} > foo - 5 95 

However, there is no standard way to redefine statements (which php / python magic methods actually do) in current versions of javascript. Harmony (Javascript 6) will include a proxy API for this kind of work. This blog post provides examples and explanations for Proxies, here an excerpt from there is similar to php __get :

 var p = Proxy.create({ get: function(proxy, name) { return 'Hello, '+ name; } }); document.write(p.World); // should print 'Hello, World' 

This already works in Firefox, Chrome support is available if you go to about:flags and enable experimental JavaScript.

+8
source

JavaScript is one of the few prototype languages , like Lua. If you are looking for a constructor , it is called that (see below). All objects in JavaScript are derived from base- Object .

Almost all of your answers regarding JavaScripts magic- "methods" are already included in the Hidden JavaScript Functions . Visit this StackOverflow site to learn a little more about JavaScript syntax and behavior, as well as features such as comparison operations .

Below are some practical examples to help you understand the behavior (and circular references when it comes to serializing JavaScript objects and importing them into PHP).

 0 .constructor function Number() { [native code] } 0 .constructor(10) >> 10 var myObj = function(){ console.log("I am a new function"); } >> undefined myObj.constructor >> function Function() { [native code] } myObj.constructor() >> function anonymous() {} myObj.prototype.constructor() >> I am a new function myObj === myObj.prototype.constructor >> true x = new myObj >> I am a new function << myObj {} x.constructor() >> I am a new function << undefined 

I wrote a description of newer languages , but there are many other useful resources, including SO, and the links are already provided by my peers here.

+2
source

To add to @ thg435's answer,

The code you wrote above can easily be rewritten as

 // this is your __construct var Person = function Person(name) { this.name = name; this.introduceYourself(); }; // this is public function introduceYourself Person.prototype.introduceYourself = function introduceYourself() { this.name ? console.log("hi my name is", this.name) : console.log("error i don't have a name") ; }; var dave = new Person("dave"); 

Short answer: there is no magic log in JavaScript and (imo), which is good. I don't like implicit things. One commenter mentions __noSuchMethod__ and yikes! MDN says it's non-standard and the page doesn't even know which JavaScript versions support it.

You might be interested in harmony proxies as part of the new ECMAScript 6. You will see some information about simulating noSuchMethod and doesNotUnderstand

+1
source

Source: https://habr.com/ru/post/953391/


All Articles