admin管理员组文章数量:1399919
In PHP, I have the following class definitions
class my_pet{
public static $name = "paulie";
public static function get_name(){
return static::$name;
}
}
class my_dog extends my_pet{
public static $name = "stacey";
}
When I echo using
echo my_dog::get_name();
I'll get "stacey".
If I change
return static::$name
to
return self::$name;
the answer turns to "paulie".
In JavaScript, I have the following object constructors
function my_pet(){
this.name = "paulie";
}
my_pet.prototype.get_name = function(){
return this.name;
}
function my_dog(){
this.name = "stacey";
}
my_dog.prototype = new my_pet();
my_dog.prototype.constructor = my_dog;
my_pet_instance = new my_dog();
When I call my method using
alert(my_pet_instance.get_name());
I will always get "stacey".
Is there a late static binding equivalent for JavaScript so I can get "paulie" instead of "stacey"?
In PHP, I have the following class definitions
class my_pet{
public static $name = "paulie";
public static function get_name(){
return static::$name;
}
}
class my_dog extends my_pet{
public static $name = "stacey";
}
When I echo using
echo my_dog::get_name();
I'll get "stacey".
If I change
return static::$name
to
return self::$name;
the answer turns to "paulie".
In JavaScript, I have the following object constructors
function my_pet(){
this.name = "paulie";
}
my_pet.prototype.get_name = function(){
return this.name;
}
function my_dog(){
this.name = "stacey";
}
my_dog.prototype = new my_pet();
my_dog.prototype.constructor = my_dog;
my_pet_instance = new my_dog();
When I call my method using
alert(my_pet_instance.get_name());
I will always get "stacey".
Is there a late static binding equivalent for JavaScript so I can get "paulie" instead of "stacey"?
Share Improve this question asked Aug 20, 2013 at 19:05 Lloyd BanksLloyd Banks 36.8k58 gold badges170 silver badges259 bronze badges 1-
2
what is your question ?
this
is always late-bound in js. But you've mademy_dog
a contructor, and then wonder about immediate consequences. – c69 Commented Aug 20, 2013 at 19:09
3 Answers
Reset to default 5In your JavaScript code, there's nothing static (or even "static-like").
To simulate static variables, you can attach properties directly to constructors:
function my_pet(){}
my_pet.name = "paulie";
function my_dog(){}
my_dog.name = "stacey";
However, to achieve what you're looking for, you may want to use the prototype chain instead:
function my_pet(){}
my_pet.prototype.name = "paulie";
function my_dog(){
this.name = "stacey";
}
my_dog.prototype = Object.create(my_pet.prototype);
var stacey = new my_dog();
console.log(stacey.name); //stacey
console.log(my_dog.prototype.name); //paulie
delete stacey.name; // no more shadowing
console.log(stacey.name); // paulie
While the semantics and mechanism are different in JavaScript, you can still access the sub-class value of the name
property from the prototype object. Try adding this line to the end of your code:
my_dog.prototype.name; // returns "paulie"
Try this Class.js javascript library.
There are implemented 2 key properties in every dynamic and static this
context - this.self
and this.static
.
It behaves in the same principle like late static binding in PHP. this.self
and this.static
contains class (prototype function) definition with build in properties Name
, Namespace
and Fullname
. Read more in project README - Features.
Syntax is possible to customize in any way you are already used to write your classes (7th demo - Class.js - example to customize syntax).
It works in all old browsers without ES6 support, in Node, in Windows Script Host environment and also in Adobe scripts.
So the code above should looking like:
Class.Define('MyPet', {
Static: {
GetStaticName: function () {
return this.static.Name;
},
GetSelfName: function () {
return this.self.Name;
}
}
});
Class.Define('MyDog', {
Extend: MyPet
});
console.log(MyPet.GetSelfName()); // MyPet
console.log(MyDog.GetSelfName()); // MyPet
console.log(MyPet.GetStaticName()); // MyPet
console.log(MyDog.GetStaticName()); // MyDog
本文标签: JavaScript Equivalent of Late Static BindingStack Overflow
版权声明:本文标题:JavaScript Equivalent of Late Static Binding - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744172289a2593838.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论