attach() inside function
Noah has already pointed out that using attach is a bad idea, even though you see it in some examples and books. There is a way around. You can use "local attach" that's called with
. In Noah's dummy example, this would look like
with(params, print(a))
which will yield identical result, but is tidier.
How to attach function that uses `this` to instance-object
To be able to use this
, you have to rewrite the arrow function to a normal function
function:
fakeWebSocket$$.multiplex = function (x, y, filterFn) {
return this.pipe(filter(filterFn));
};
This usage is not documented, but it should work.
The documentation however is written in such a way that suggests creating a derived class to override multiplex()
, rather than assigning a new value directly to the instance:
class FakeSubject<T> extends WebSocketSubject<T> {
override multiplex(
subMsg: () => any,
unsubMsg: () => any,
messageFilter: (value: T) => boolean
) {
return this.pipe(filter(messageFilter));
}
}
const fakeWebSocket$$ = new FakeSubject();
Using identify and attach in a function
You are trying to do non-standard evaluation here. This is always a little tricky. Can I recommend something like:
f <- function(x, y, data) {
dx <- deparse(substitute(x))
dy <- deparse(substitute(y))
plot(reformulate(dx,response=dy),data)
ids <- identify(data[,dx], data[,dy])
return(ids)
}
f(Population,Income,state.x77)
or
f2 <- function(x, y, data) {
x <- eval(substitute(x),envir=as.data.frame(data))
y <- eval(substitute(y),envir=as.data.frame(data))
plot(x,y)
ids <- identify(x,y)
return(ids)
}
f2(Population,Income,state.x77)
You might want to look at Hadley Wickham's notes on non-standard evaluation for more information ...
Attach function as a method to another function in JS
Assign fetchIt
to a variable before returning, then add the function on there:
function outer (num, time) {
...
let fetchIt = function fetchIt() {
...
}
fetchIt.reset = function reset() {
count = 1 // Or whatever you need to do
}
return fetchIt
}
How to attach method to method of a function
var A = function (z) {
this.z = z
}
// attach 1 method
A.prototype.sayHay = function(message) {
this.message = message;
return {
log : function(){console.log(message)},
rev : function () { console.log(this.message.split('').reverse().join(''))}
};
}
var b = new A();
b.sayHay('Hola').rev();
// this should prints 'aloH'
I have tried to do it in way of closures. Check if you get satisfied to achieve your goal with prototype and closure combination.
How to attach model functions to model results
there isn't any way; because when you declare a function in a model it works with that object; and loads the function result when you call the function or appended function; so for doing this job you should write a foreach for loading the values for each object
Related Topics
Ggplot2 Add a Legend for Several Stat_Functions
How to Ensure That a Partition Has Representative Observations from Each Level of a Factor
R - Reading Lines from a .Txt-File After a Specific Line
Drawing a Stratified Sample in R
Filled and Hollow Shapes Where the Fill Color = the Line Color
Nested Facet Plot with Ggplot2
How to Remove Nas with the Tidyr::Unite Function
Changing Class and Mode from Character to Numeric
How to Transpose a Tibble() in R
Fast Way to Group Variables Based on Direct and Indirect Similarities in Multiple Columns
Select Last Row by Group for All Columns Data.Table
Merge Records Over Time Interval