Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
类的JavaScript实例_Javascript - Fatal编程技术网

类的JavaScript实例

类的JavaScript实例,javascript,Javascript,假设我有一个函数: function Control(value){ var self=this; self.param1=value; self.param2=value.text; } 是否可以在JavaScript中获取该函数创建的所有实例?否 事实上,您只能触摸有显式引用的对象* *该安全模型不适用于全局对象的JavaScript。否 事实上,您只能触摸有显式引用的对象* *该安全模型不适用于全局对象的JavaScript。只需将其存储在全局数组中即可。将类似于

假设我有一个函数:

function Control(value){
    var self=this;
    self.param1=value;
    self.param2=value.text;
}
是否可以在JavaScript中获取该函数创建的所有实例?

事实上,您只能触摸有显式引用的对象*

*该安全模型不适用于全局对象的JavaScript。

事实上,您只能触摸有显式引用的对象*


*该安全模型不适用于全局对象的JavaScript。

只需将其存储在全局数组中即可。将类似于一些静态模式

var controls = [] ; //Global array

function Control(value){
    var self = this;
    self.param1=value;
    self.param2=value.text;

   controls.push(self);
}

只需将其存储在全局数组中。将类似于一些静态模式

var controls = [] ; //Global array

function Control(value){
    var self = this;
    self.param1=value;
    self.param2=value.text;

   controls.push(self);
}

如果不修改该类,您将无能为力。如果可以修改,只需保留对所构造的每个实例的引用:

function Control(value){
    Control.instances = Control.instances || [];
    Control.instances.push(this);

    var self=this;
    self.param1=value;
    self.param2=value.text;
}

// In case you are worried about garbage collection
Control.prototype.destroy = function() {
    var index = Control.instances.indexOf(this);
    Control.instances.splice(index, 1);
}


// Usage:
var x = new Control();
var y = new Control();
// do stuff with x and y
console.log('There are ' + Control.instances.length + ' instances of Control');
// all done with x and y
x.destroy();
x = null;
y.destroy();
y = null;
// no more references to the two instances, they can be garbage collected

不过,请注意,您将阻止垃圾收集器释放任何未调用destroy()的实例的内存。

如果不修改该类,您将无能为力。如果可以修改,只需保留对所构造的每个实例的引用:

function Control(value){
    Control.instances = Control.instances || [];
    Control.instances.push(this);

    var self=this;
    self.param1=value;
    self.param2=value.text;
}

// In case you are worried about garbage collection
Control.prototype.destroy = function() {
    var index = Control.instances.indexOf(this);
    Control.instances.splice(index, 1);
}


// Usage:
var x = new Control();
var y = new Control();
// do stuff with x and y
console.log('There are ' + Control.instances.length + ' instances of Control');
// all done with x and y
x.destroy();
x = null;
y.destroy();
y = null;
// no more references to the two instances, they can be garbage collected

但是请注意,您将阻止垃圾收集器释放任何未调用destroy()的实例的内存。

正如@Ian在其评论中建议的那样,您可以这样做:

// global are bad though!
var controlInstances = [];

function Control(value){
    // track instances
    controlInstances.push(this)
    this.param1 = value;
    thsi.param2 = value.text;
}

正如@Ian在他的评论中所建议的,你可以这样做:

// global are bad though!
var controlInstances = [];

function Control(value){
    // track instances
    controlInstances.push(this)
    this.param1 = value;
    thsi.param2 = value.text;
}

我在我的一个项目中使用了类似的方法,因为我需要为所有实例调用一个方法

function Person(firstname, lastname) {
    var self = this; // For reference to "this" 
    this.firstname = firstname;
    this.lastname = lastname;


    document.addEventListener( "event/person", function( event ) {
        var params = event.detail.params;
        Person.prototype[ params.method ].call(self);

       // Take params:   params.params => ["param1", "param2", "param3"]
    });
}


Person.prototype.toString = function() {
    console.log("Firstname: " + this.firstname + " Lastname: " + this.lastname);  
};


Person.prototype.callAll = function( params ) {
    document.dispatchEvent( new CustomEvent( "event/person", {
        detail: {
            "params": params   
        }
    }));
};


var a = new Person("Gabriel", "Gatu");
var b = new Person("Marco", "Giovannini");


Person.prototype.callAll({
    method: "toString",
    params: ["param1", "param2", "param3"]
});
function Person(firstname, lastname) {
    var self = this; // For reference to "this" 
    this.firstname = firstname;
    this.lastname = lastname;


    document.addEventListener( "event/person", function( event ) {
        var params = event.detail.params;
        Person.prototype[ params.method ].call(self);

       // Take params:   params.params => ["param1", "param2", "param3"]
    });
}


Person.prototype.toString = function() {
    console.log("Firstname: " + this.firstname + " Lastname: " + this.lastname);  
};


Person.prototype.callAll = function( params ) {
    document.dispatchEvent( new CustomEvent( "event/person", {
        detail: {
            "params": params   
        }
    }));
};


var a = new Person("Gabriel", "Gatu");
var b = new Person("Marco", "Giovannini");


Person.prototype.callAll({
    method: "toString",
    params: ["param1", "param2", "param3"]
});
它使用监听器在简单方法和构造函数类之间进行通信,还允许您调用类原型中包含的任何方法,并在必要时传递参数


p、 对不起,英语不好……:)

我在我的一个项目中使用了类似的方法,因为我需要为所有实例调用一个方法

function Person(firstname, lastname) {
    var self = this; // For reference to "this" 
    this.firstname = firstname;
    this.lastname = lastname;


    document.addEventListener( "event/person", function( event ) {
        var params = event.detail.params;
        Person.prototype[ params.method ].call(self);

       // Take params:   params.params => ["param1", "param2", "param3"]
    });
}


Person.prototype.toString = function() {
    console.log("Firstname: " + this.firstname + " Lastname: " + this.lastname);  
};


Person.prototype.callAll = function( params ) {
    document.dispatchEvent( new CustomEvent( "event/person", {
        detail: {
            "params": params   
        }
    }));
};


var a = new Person("Gabriel", "Gatu");
var b = new Person("Marco", "Giovannini");


Person.prototype.callAll({
    method: "toString",
    params: ["param1", "param2", "param3"]
});
function Person(firstname, lastname) {
    var self = this; // For reference to "this" 
    this.firstname = firstname;
    this.lastname = lastname;


    document.addEventListener( "event/person", function( event ) {
        var params = event.detail.params;
        Person.prototype[ params.method ].call(self);

       // Take params:   params.params => ["param1", "param2", "param3"]
    });
}


Person.prototype.toString = function() {
    console.log("Firstname: " + this.firstname + " Lastname: " + this.lastname);  
};


Person.prototype.callAll = function( params ) {
    document.dispatchEvent( new CustomEvent( "event/person", {
        detail: {
            "params": params   
        }
    }));
};


var a = new Person("Gabriel", "Gatu");
var b = new Person("Marco", "Giovannini");


Person.prototype.callAll({
    method: "toString",
    params: ["param1", "param2", "param3"]
});
它使用监听器在简单方法和构造函数类之间进行通信,还允许您调用类原型中包含的任何方法,并在必要时传递参数


p、 对不起,英语不好……:)

我在我的一个项目中使用了类似的方法,因为我需要为所有实例调用一个方法

function Person(firstname, lastname) {
    var self = this; // For reference to "this" 
    this.firstname = firstname;
    this.lastname = lastname;


    document.addEventListener( "event/person", function( event ) {
        var params = event.detail.params;
        Person.prototype[ params.method ].call(self);

       // Take params:   params.params => ["param1", "param2", "param3"]
    });
}


Person.prototype.toString = function() {
    console.log("Firstname: " + this.firstname + " Lastname: " + this.lastname);  
};


Person.prototype.callAll = function( params ) {
    document.dispatchEvent( new CustomEvent( "event/person", {
        detail: {
            "params": params   
        }
    }));
};


var a = new Person("Gabriel", "Gatu");
var b = new Person("Marco", "Giovannini");


Person.prototype.callAll({
    method: "toString",
    params: ["param1", "param2", "param3"]
});
function Person(firstname, lastname) {
    var self = this; // For reference to "this" 
    this.firstname = firstname;
    this.lastname = lastname;


    document.addEventListener( "event/person", function( event ) {
        var params = event.detail.params;
        Person.prototype[ params.method ].call(self);

       // Take params:   params.params => ["param1", "param2", "param3"]
    });
}


Person.prototype.toString = function() {
    console.log("Firstname: " + this.firstname + " Lastname: " + this.lastname);  
};


Person.prototype.callAll = function( params ) {
    document.dispatchEvent( new CustomEvent( "event/person", {
        detail: {
            "params": params   
        }
    }));
};


var a = new Person("Gabriel", "Gatu");
var b = new Person("Marco", "Giovannini");


Person.prototype.callAll({
    method: "toString",
    params: ["param1", "param2", "param3"]
});
它使用监听器在简单方法和构造函数类之间进行通信,还允许您调用类原型中包含的任何方法,并在必要时传递参数


p、 对不起,英语不好……:)再见

我在我的一个项目中使用了类似的方法,因为我需要为所有实例调用一个方法

function Person(firstname, lastname) {
    var self = this; // For reference to "this" 
    this.firstname = firstname;
    this.lastname = lastname;


    document.addEventListener( "event/person", function( event ) {
        var params = event.detail.params;
        Person.prototype[ params.method ].call(self);

       // Take params:   params.params => ["param1", "param2", "param3"]
    });
}


Person.prototype.toString = function() {
    console.log("Firstname: " + this.firstname + " Lastname: " + this.lastname);  
};


Person.prototype.callAll = function( params ) {
    document.dispatchEvent( new CustomEvent( "event/person", {
        detail: {
            "params": params   
        }
    }));
};


var a = new Person("Gabriel", "Gatu");
var b = new Person("Marco", "Giovannini");


Person.prototype.callAll({
    method: "toString",
    params: ["param1", "param2", "param3"]
});
function Person(firstname, lastname) {
    var self = this; // For reference to "this" 
    this.firstname = firstname;
    this.lastname = lastname;


    document.addEventListener( "event/person", function( event ) {
        var params = event.detail.params;
        Person.prototype[ params.method ].call(self);

       // Take params:   params.params => ["param1", "param2", "param3"]
    });
}


Person.prototype.toString = function() {
    console.log("Firstname: " + this.firstname + " Lastname: " + this.lastname);  
};


Person.prototype.callAll = function( params ) {
    document.dispatchEvent( new CustomEvent( "event/person", {
        detail: {
            "params": params   
        }
    }));
};


var a = new Person("Gabriel", "Gatu");
var b = new Person("Marco", "Giovannini");


Person.prototype.callAll({
    method: "toString",
    params: ["param1", "param2", "param3"]
});
它使用监听器在简单方法和构造函数类之间进行通信,还允许您调用类原型中包含的任何方法,并在必要时传递参数


p、 对不起,英语不好……:)再见

我能想到的唯一简单的方法是在某个全局数组中存储对任何创建的
控件
类的引用。您可以在
控件
构造函数(上面的函数)中进行存储。据我所知,没有这样的事情。您需要有自己的机制来保存Control class.nope的所有实例(而不需要自己跟踪它们)。否则,垃圾收集器就不可能存在。可能的重复也请注意JavaScript中没有类。我能想到的唯一简单的方法是在某个全局数组中存储对任何创建的
控件
类的引用。您可以在
控件
构造函数(上面的函数)中进行存储。据我所知,没有这样的事情。您需要有自己的机制来保存Control class.nope的所有实例(而不需要自己跟踪它们)。否则,垃圾收集器将不存在。的可能副本还请注意JavaScript中没有类。这与问题有什么关系?这与问题有什么关系?
Person.prototype[params.method]。调用(self)
=
self[params.method]()
当实例超出范围时,事件侦听器仍在那里。
Person.prototype[params.method]。调用(self)=
self[params.method]()
当您的实例超出范围时,事件侦听器仍然存在。如果您担心垃圾收集器,我认为您必须简单地将destroy方法添加到类原型中,并将侦听器与该对象分离。你怎么看?如果你担心垃圾收集器,我认为你必须简单地将destroy方法添加到类原型中,并将侦听器与该对象分离。你觉得怎么样?