Reputation: 3199
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?
Upvotes: 5
Views: 714
Reputation:
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 $this
inside does not change depending on how the method was invoked.
In JavaScript, however, object.member(...)
is equivalent to object["member"].call(object, ...)
: the this
inside the JavaScript method is entirely dependent upon how the function is invoked. (This is why callbacks in JavaScript sometimes require closures to pass this
through 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.
Upvotes: 9
Reputation: 5536
Your question is "are they similar" – the answer is "yes."
The nuances of prototypal or classical inheritance are interesting, sure, but for your purposes (especially if you're at the point of asking this question) you should proceed as if they're equivalent. Enjoy the world of Javascript, its a ton of fun. When you're ready to ask more questions, read a comparison with class-based models and enjoy the nuances!
Upvotes: 1
Reputation: 1
First of all, PHP is Server Side whereas Javascript is Client Side. So decide which side you want your methods or variable should work?
Come to your core Question : " is “->” in php similar to “.” in javascript? "
The answer is No. Because in PHP, You Have to make an Object of a class first & then You can use that object. Whereas in JS(JavaScript) there is no need of Class.
Use of both PHP and JS in appropriate manner can help u a lot to make a nice site...
Upvotes: -1
Reputation: 490173
Pretty much it is, both allow you to access properties and methods on objects.
Remember that JavaScript has a quite different object system which is class-less out of the box.
Upvotes: 4