定义点函数,如jquery的“.map()”、“.each()”等

a6b3iqyw  于 2021-09-23  发布在  Java
关注(0)|答案(1)|浏览(212)

我有两个函数和一个构造函数,定义如下:

let mx = function(arr) {
    return new mx.fn.init(arr) 
}

mx.fn = mx.prototype = {
    constructor: mx,
}

init = mx.fn.init = function(arr) {
    //do things and return an object containing data about arr
}

因此,这段代码运行良好,可以调用 mx(array) 返回所需的对象。现在,我如何定义函数来操作这个对象?我想定义函数,比如, mx(array).addRow(row) 更改由返回的对象中的数据 mx(array) 但我无法做到这一点。我试着用英语来定义它 mx.fn 这样地: addRow: function(arr) { //do smth } 但它不起作用。我也试着去做 mx.prototype.addRow = function(row) { //do smth } . 你知道这是否可行吗?它看起来像jquery的 $('#id').css('color': 'red') 很多,但我不确定这是否同样有效。我对这些概念都很陌生,所以我对所有这些原型都有点迷茫。。。
提前感谢您的帮助!

zxlwwiss

zxlwwiss1#

你需要设置 prototypeinit 功能。

let mx = function(arr) {
    return new mx.fn.init(arr) 
}
let init = function(arr) {
    //do things and return an object containing data about arr
}
mx.fn = init.prototype = {
    addRow(row){
        // do something
    },
    init: init
}

相关问题