Fluoresce Posted May 8, 2009 Share Posted May 8, 2009 I've recently started learning JavaScript. I already know a bit of PHP. I've noticed that the two languages are very similar. This is strange because I've never heard anybody say that they are similar. I'm assuming that this is because they become increasingly disimilar as you learn more? Is that correct? Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/ Share on other sites More sharing options...
Mchl Posted May 8, 2009 Share Posted May 8, 2009 They have similar syntax at times (in both cases derived from C syntax), but they differ a lot in what (and also how) they can do. Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/#findComment-829491 Share on other sites More sharing options...
KevinM1 Posted May 8, 2009 Share Posted May 8, 2009 I've recently started learning JavaScript. I already know a bit of PHP. I've noticed that the two languages are very similar. This is strange because I've never heard anybody say that they are similar. I'm assuming that this is because they become increasingly disimilar as you learn more? Is that correct? Like Mchl said, their similarities lie in their syntax. Other languages have a similar syntax as well (C, C++, C#, Java, Perl, etc.). The two are completely different, otherwise. PHP is a server-side language. Therefore, it can't read and respond to browser events (button presses, mouse movement or clicks, etc.) directly. JavaScript is client-side. It runs in the browser and allows sites to be more interactive by responding to the kinds of browser events I mentioned earlier. Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/#findComment-829515 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 JavaScript is a little like PHP OOP. Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/#findComment-829522 Share on other sites More sharing options...
Mchl Posted May 8, 2009 Share Posted May 8, 2009 JavaScript is a little like PHP OOP. Very little I'd be tempted to say... Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/#findComment-829526 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 I should have said JavaScript is a little like PHP OOP without the use of classes. My bad. Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/#findComment-829534 Share on other sites More sharing options...
Mchl Posted May 8, 2009 Share Posted May 8, 2009 You say strange things... A 'class' is a core concept in OOP. Can't have OOP without classes... I think what you mean, is that JS is much more object oriented than PHP. Just about everything in JS script is an object (including variables). This is approach taken from Java (hence the name). Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/#findComment-829540 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Well no, you can do JavaScript OOP without "classes". Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/#findComment-829613 Share on other sites More sharing options...
Maq Posted May 8, 2009 Share Posted May 8, 2009 Well no, you can do JavaScript OOP without "classes". Really, how? Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/#findComment-829619 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 function Person (name, age) { this.name = name; this.age = age; } Person.prototype.getName = function () { return this.name; } Person.prototype.getAge = function () { return this.age; } var Maq = new Person("Maq", 17); // just putting some age alert(Maq.getName()); alert(Maq.getAge()); That good enough or should I keep going? Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/#findComment-829623 Share on other sites More sharing options...
Daniel0 Posted May 8, 2009 Share Posted May 8, 2009 It is a class though it's declared implicitly in Javascript. You cannot have OOP without classes. Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/#findComment-829643 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 It is a class though it's declared implicitly in Javascript. You cannot have OOP without classes. You can call it that if you want, but there aren't any classes in JavaScript. Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/#findComment-829675 Share on other sites More sharing options...
Mchl Posted May 8, 2009 Share Posted May 8, 2009 The fact that you don't have to use 'class' keyword to define a class in JS, does not mean there are no classes... I'll ask the other way round: What this piece of code you posted does? Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/#findComment-829681 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 It alerts a string followed by another alert of a number. The point was to illustrate the difference between PHP and JavaScript. There isn't this class thing where you can just put all your variables and functions in. Logically, you can view it as a class. It just isn't to me. In the end, it's still a function that you can use and call without making it OOP. I just didn't expand on the function to have it do that. Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/#findComment-829708 Share on other sites More sharing options...
GingerRobot Posted May 8, 2009 Share Posted May 8, 2009 There isn't this class thing where you can just put all your variables and functions in. If you view a class as simply a wrapper for variables and functions, your view of OOP is way off. Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/#findComment-829712 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 I do view a class as such because it looks exactly like that. It has some variables and some functions. Breaking it down, that's pretty much all you see in a class. Of course the idea behind it and its usage is different. Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/#findComment-829731 Share on other sites More sharing options...
Mchl Posted May 8, 2009 Share Posted May 8, 2009 I meant this piece of code function Person (name, age) { this.name = name; this.age = age; } Person.prototype.getName = function () { return this.name; } Person.prototype.getAge = function () { return this.age; } Ok, so you'll say 'defines a function and two prototypes'. But what's the point? The point is to describe a certain class of objects you can create and use later. A class is fundamental concept of object oriented programming. JavaScript just has a bit wicked way of declaring them. [edit] Actually also wicked way of making use of them. Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/#findComment-829732 Share on other sites More sharing options...
Daniel0 Posted May 8, 2009 Share Posted May 8, 2009 Yeah ECMA-262 says: ECMAScript does not contain proper classes such as those in C++' date=' Smalltalk, or Java, but rather, supports constructors which create objects by executing code that allocates storage for the objects and initialises all or part of them by assigning initial values to their properties.[/quote'] However, the same document refers to an internal property of all objects called Class numerous times. See e.g. p. 39: [[Class]]: A string value indicating the kind of this object. As far as I'm concerned, Javascript does operate with classes though they decided to call it prototypes instead. Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/#findComment-829740 Share on other sites More sharing options...
Mchl Posted May 8, 2009 Share Posted May 8, 2009 Technically that's correct. JavaScript uses concept of prototypes, which differ in behaviour from classes as known from PHP or Java. (and described by me as 'wicked' ) However the goal of both a 'class' and a 'prototype' is ultimately the same. On higher level of abstraction they are essentially the same. Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/#findComment-829742 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 That's exactly right. I can see the concept of classes in JavaScript, but they're not classes in my POV. Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/#findComment-829743 Share on other sites More sharing options...
Fluoresce Posted May 9, 2009 Author Share Posted May 9, 2009 What the . . . ! I thought, let me take a quick look to see if anyone's responded to my thread. Instead of finding a response, I found two pages of arguments! Interesting cases, guys. Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/#findComment-830084 Share on other sites More sharing options...
Maq Posted May 9, 2009 Share Posted May 9, 2009 What the . . . ! I thought, let me take a quick look to see if anyone's responded to my thread. Instead of finding a response, I found two pages of arguments! Interesting cases, guys. Yes, seems as if we semi hi-jacked your thread in a brief debate, but hopefully you've learned a thing or two from thread, I have. That's exactly right. I can see the concept of classes in JavaScript, but they're not classes in my POV. I guess we can all agree that you need the concept of 'classes' in order to achieve OOP. Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/#findComment-830130 Share on other sites More sharing options...
nrg_alpha Posted May 9, 2009 Share Posted May 9, 2009 What the . . . ! I thought, let me take a quick look to see if anyone's responded to my thread. Instead of finding a response, I found two pages of arguments! Interesting cases, guys. Yes, seems as if we semi hi-jacked your thread in a brief debate... This would be akin to you flying to a specific destination, only to have the pilots (during autopilot mid flight) start getting into a debate about who's wife is better and realize after some time that they have over shot their destination, and with the amount of fuel they have left, are forced to land somewhere else. Thank you for flying 'OOP? Airways'. We apologize for not getting you to your destination. It appears our flying methods were overridden. We promise to have better class next time :-\ Quote Link to comment https://forums.phpfreaks.com/topic/157366-solved-php-and-javascript-just-how-similar-are-they/#findComment-830404 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.