定义点函数,如 jQuery 的“.map()”、“.each()”等
Define dot functions like jQuery's ".map()", ".each()", etc
我有 2 个函数和一个构造函数,定义如下:
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)
returns 想要的对象。
现在,我如何定义函数来操作这个对象?我想定义函数,例如 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')
,但我不确定这是否同样有效。
我对这些概念中的很多都是陌生的,所以我对所有这些原型有点迷茫......
在此先感谢您的帮助!
您需要设置init
函数的prototype
。
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
}
我有 2 个函数和一个构造函数,定义如下:
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)
returns 想要的对象。
现在,我如何定义函数来操作这个对象?我想定义函数,例如 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')
,但我不确定这是否同样有效。
我对这些概念中的很多都是陌生的,所以我对所有这些原型有点迷茫......
在此先感谢您的帮助!
您需要设置init
函数的prototype
。
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
}