最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript Equivalent of Late Static Binding - Stack Overflow

programmeradmin7浏览0评论

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 made my_dog a contructor, and then wonder about immediate consequences. – c69 Commented Aug 20, 2013 at 19:09
Add a ment  | 

3 Answers 3

Reset to default 5

In 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
发布评论

评论列表(0)

  1. 暂无评论