python - sandwich pattern in javascript code -
apologize if title of question misleading. looking javascript equivalent of following python code:
## python code def call_with_context(fn, *args): ## code create context, e.g. profiling, db.connect, or drawing context store stack fn(*args) ## code close context
this implements similiar functionality "with statement" in python, implements aspect-oriented paradigm.
so question javascript way of doing such things? have seen code using array.prototype.slice(arguments, 1) so, don't know if common pattern in javascript, or there better patterns supported in javascript (e.g. closure) ppl don't that. pls correct me if using wrong keywords, because dont know how refer problem better name sandwich.
edt 1: , appreciate if can explain how return result of fn(*args) inside wrapper call_with_context. thanks!
i think more typical js way of doing might decorate function. if wanted wrap function in logged timing, might create function (off top of head):
var createtimer = function(fn) { return function() { var start = new date(); var result = fn.apply(this, arguments); console.log("took " + (new date() - start) + " ms."); return result; } }; var test = function(a, b, c) { return * b + c; } test = createtimer(test); console.log(test(3, 4, 5)); // took 0 ms. // 17
the main point might not call this:
runtimeraround(test, 3, 4, 5);
although done in js, is, believe less common overwriting functions directly.
Comments
Post a Comment