问题描述
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')
,但我不确定这是否以相同的方式工作。
我对很多这些概念都不熟悉,所以我对所有这些原型都有点迷失...
预先感谢您的帮助!
解决方法
您需要设置prototype
函数的init
。
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
}