Reputation: 8058
I am learning some Javascript but I have a question about prototype
Is prototype in this case similar to PHP static method since all the instances share it?
Javascript
var Person = function(){
this.name = "Test Person";
}
Person.prototype.work = function(){
return this.name + "is working";
}
PHP
class Person($personName){
public static working(){
return $personName . "is working";
}
}
$person1 = new Person("Joe").working();
$person2 = new Person("Mike").wokring();
Upvotes: 1
Views: 867
Reputation: 26696
The technical answer is no. The underlying implementation of a prototyped function is not the same as the implementation of a static method in a C-like language.
...however, in practice, you can consider their general usage to be similar to public-static
methods.
More similar would be prototyped properties compared to public-static members.
They aren't called the same way, but the concept of a method or a property which sits on the "class"/constructor, and is accessible to each instance, and modifying these values, as shared values, impacting all other instances is very, very much like a public static
.
There are a few other things to keep in mind, though, and this will either agree with what you consider the functionality of public static
to be, depending on the languages you use...
The most important, of course, is that in terms of prototyped functions, they have absolutely no access to any private/instance-specific state, which is not publicly-accessible.
So if you're used to dealing with public static
functions which might be able to call private methods or use instance-specific variables (which aren't public members), then you're 120% out of luck.
The second thing to consider is that you can also create the equivalent of public and private static members/methods through closure.
var Class = (function () {
var private_static_method = function () {},
public_static_method = function () {},
private_static_property = 1,
public_static_property = 2,
class_constructor = function (a, b) {
var private_method = function () {},
public_method = function () {},
public_property = a,
private_property = b,
public_interface = {
method : public_method,
property : public_property
};
return public_interface;
};
class_constructor.method = public_static_method;
class_constructor.property = public_static_property;
return class_constructor;
}());
Now, through closure, I've got "private static" functions and properties, which every private instance can access.
I've got public static methods, which I've appended as methods of the constructor function.
Then I've got the constructor function which doesn't use new
, and just creates a new object (public_interface
), which attaches whatever you want.
Any of the public methods of the instance can access any private methods/variables of that instance, thanks to closure.
None of this has anything to do with traditional classes.
But from a purely closure-based standpoint, this is how you could recreate all of these access-types, without driving yourself crazy, trying to shoehorn classes into JS.
Upvotes: 2
Reputation: 12966
Not really, no. I recommend reading through Doug Crockford's article on Prototypal Inheritance to understand what x.prototype
means in Javascript, and how it can relate to more "classical" inheritance. Wikipedia also has an entry that may help you with the concept.
Upvotes: 2