admin管理员组文章数量:1356795
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?
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?
Share Improve this question edited Dec 28, 2011 at 5:42 user asked Dec 28, 2011 at 3:32 useruser 3,1995 gold badges27 silver badges39 bronze badges 2- It's similar, yes. But JS doesn't have classes like PHP does. – powerbuoy Commented Dec 28, 2011 at 3:34
-
NB:
->
is called the "arrow operator", ando.p
is called "dot notation" (as opposed to "bracket notation":o[s]
, wheres=='p'
). I don't believe there's an official name for.
in JS, though you could call it the "dot operator" and be understood. Using->
,.
or[]
(in their appropriate languages) is called "property access" or "member access" (the former term in PHP is usually restricted to refer to accessing object fields, rather than methods). – outis Commented Dec 28, 2011 at 3:50
4 Answers
Reset to default 9Similar, 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.
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.
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 parison with class-based models and enjoy the nuances!
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...
本文标签: is quotgtquot in php similar (aka equivalent) to quotquot in javascriptStack Overflow
版权声明:本文标题:is "->" in php similar (a.k.a equivalent) to "." in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744028048a2578397.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论