FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mathjs/examples/function_transform.js at v1_develop · ThinkLib/mathjs · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
ThinkLib
/
mathjs
Public
forked from
josdejong/mathjs
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
mathjs
/
examples
/
function_transform.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
49 lines (41 loc) · 1.22 KB
Breadcrumbs
mathjs
/
examples
/
function_transform.js
Copy path
File metadata and controls
49 lines (41 loc) · 1.22 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* Function transforms
*
* When using functions via the expression parser, it is possible to preprocess
* function arguments and post process a functions return value by writing a
* *transform* for the function. A transform is a function wrapping around a
* function to be transformed or completely replaces a function.
*/
var
math
=
require
(
'../index'
)
;
// create a function
function
addIt
(
a
,
b
)
{
return
a
+
b
;
}
// attach a transform function to the function addIt
addIt
.
transform
=
function
(
a
,
b
)
{
console
.
log
(
'input: a='
+
a
+
', b='
+
b
)
;
// we can manipulate the input arguments here before executing addIt
var
res
=
addIt
(
a
,
b
)
;
console
.
log
(
'result: '
+
res
)
;
// we can manipulate the result here before returning
return
res
;
}
;
// import the function into math.js
math
.
import
(
{
addIt
:
addIt
}
)
;
// use the function via the expression parser
console
.
log
(
'Using expression parser:'
)
;
console
.
log
(
'2+4='
+
math
.
eval
(
'addIt(2, 4)'
)
)
;
// This will output:
//
// input: a=2, b=4
// result: 6
// 2+4=6
// when used via plain JavaScript, the transform is not invoked
console
.
log
(
''
)
;
console
.
log
(
'Using plain JavaScript:'
)
;
console
.
log
(
'2+4='
+
math
.
addIt
(
2
,
4
)
)
;
// This will output:
//
// 6
Back
|
FazBrowse Home
|
New Git URL