在原型中定义私有变量,这些变量不能在孩子之间共享

Defining private variables in prototype that are not shared among children and other prototyping tricks

本文关键字:变量 不能 孩子 之间 共享 定义 原型      更新时间:2023-09-26

我希望为每个子节点构建私有变量,而不是共享。(这是我最少需要的…)

function AbstractClass(){
var private_var; // not shared
// todo : how to create a static(shared) variable?
this.virtual_method = function(){};
this.some_fun = function(){
   console.log(private_var);
}
// todo : how to access static(shared) variable?
}

这个基抽象类应该足够方便,可以从中构造许多子类

function Child1(param){
    private_var = param;
    this.virtual_method = function(){alert('child1');}; //redefining
    this.some_fun();
}

var first_child = new Child1(5); //console : 5
var second_child = new Child1(16); //console : 16
first_child.some_fun() //console : 5;
second_child.some_fun() //console : 16;
fist_child.virtual_method();  // alert

请帮帮我…我需要一些工作代码是我的指南

如果你指的是库prototype.js,看这里。(我猜你做,因为原生javascript没有类)。