In other words, php
$object->method();
and
$object->property = 'someValue';
is equivalent to, js:
$object.method();
and
$object.property = 'someValue';
I am curious, or is my php and js understanding messed up?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Similar, and yet so different.
One big — but not exclusive! — difference is that, in PHP, methods are bound to an instance of a class, while in JavaScript, methods are just functions (which are first-class-values) that happen to be named by (“stored in”) properties of objects.
Since PHP methods are bound to an instance of a class, this means that
$thisinside does not change depending on how the method was invoked.In JavaScript, however,
object.member(...)is equivalent toobject["member"].call(object, ...): thethisinside the JavaScript method is entirely dependent upon how the function is invoked. (This is why callbacks in JavaScript sometimes require closures to passthisthrough correctly.)As you continue to learn/use both languages (and hopefully different languages entirely!), you will be able to see more similarities and differences in both fundamental design differences and syntax. Learning to “respect” a language, for what it is and how it does things, is a good way to be friends with it.
Happy coding.