Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.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_Html_Oop - Fatal编程技术网

Javascript 需要复制代码,而不是实例化多个对象并将代码保留在一个位置

Javascript 需要复制代码,而不是实例化多个对象并将代码保留在一个位置,javascript,html,oop,Javascript,Html,Oop,我开发了一些javascript,它从数组生成一个动态表,然后允许用户编辑数据。这很好用 现在我想在同一个HTML页面上使用这段代码3次。目前,它取决于两个变量:元数据,它描述了标题格式,并且是只读的,在所有三种情况下都是相同的;以及“数据”,它实际上保存了表的数据 与每个实例都有三份代码副本相比,我认为最好有一个对象,它将所有内容都保存在内部,并且只包含“setData”和“getData”方法 现有代码动态创建onclick事件-如何让这些事件引用objects函数,而不是全局函数?(也就是

我开发了一些javascript,它从数组生成一个动态表,然后允许用户编辑数据。这很好用

现在我想在同一个HTML页面上使用这段代码3次。目前,它取决于两个变量:元数据,它描述了标题格式,并且是只读的,在所有三种情况下都是相同的;以及“数据”,它实际上保存了表的数据

与每个实例都有三份代码副本相比,我认为最好有一个对象,它将所有内容都保存在内部,并且只包含“setData”和“getData”方法

现有代码动态创建onclick事件-如何让这些事件引用objects函数,而不是全局函数?(也就是说,目前有像'RemoveRow(index)'这样的函数,我假设它是'obj.RemoveRow(index)')。我很想在jquery中这样做,但遗憾的是,除非有人有一个解决方案,否则我不知道要传入的行索引。也许将它存储在DOM中的某个地方,并以某种方式访问它以确定单击的行


是否可以将引用传递给数组?如果对象可以操纵传入的数组,而不是复制一个副本,然后调用者必须将其复制回自己的数组,那会更好。

因此这是非常原始的代码,只是多个选项中的一个,但您可能会想到:

创建处理单个表的对象

function TableControl () {
    // dom elements
    this.table = null;
    this.rows = null;
    // values
    this.id = null;
    // create new row
    this.insertRow = function() {
        // some logic
        this.table.append( 'new row goes here' );
    },
    // load table object and rows and stuff
    this._initiate = function() {
        //create object with all passed arguments
        var args = arguments[0] || {};

        // set internal values
        this.id = args.id;

        // get dom elements
        this.table = $('#' + this.id);
        this.rows = this.table.find('tr');
    },
    this._initiate( arguments[0] );
}
var table_1 = new TableConrol({ id: 'table_1' });
var table_2 = new TableConrol({ id: 'table_2' });
var table_3 = new TableConrol({ id: 'table_3' });
使用每个表的
id
创建该对象的实例

function TableControl () {
    // dom elements
    this.table = null;
    this.rows = null;
    // values
    this.id = null;
    // create new row
    this.insertRow = function() {
        // some logic
        this.table.append( 'new row goes here' );
    },
    // load table object and rows and stuff
    this._initiate = function() {
        //create object with all passed arguments
        var args = arguments[0] || {};

        // set internal values
        this.id = args.id;

        // get dom elements
        this.table = $('#' + this.id);
        this.rows = this.table.find('tr');
    },
    this._initiate( arguments[0] );
}
var table_1 = new TableConrol({ id: 'table_1' });
var table_2 = new TableConrol({ id: 'table_2' });
var table_3 = new TableConrol({ id: 'table_3' });
因此,每个表只处理其自身“范围”内的内容,例如添加新行,例如:

table_1.insertRow();