Skip to content

Why javascript evaluation context inside a mixin is not equals the outter context? #1139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
RocHo opened this issue Jan 22, 2013 · 6 comments

Comments

@RocHo
Copy link

RocHo commented Jan 22, 2013

I wrote some helper functions in root less file:

@randomFunctions : ~(function(s){ var round = function(n,c){ c = c|0; return Math.round(n * Math.pow(10,c))/Math.pow(10,c); }; var random = function(s,e){ return Math.random() * (e - s) + s; }; global.r = function(s,e,u){ return round(random(s,e)) + u; }; return 'randomFunctions'; })(this);

then I could generate random numbers using these functions:

.r{ opacity : ~r(0,1); } outputs .rr{ opacity : 0.3; }

but in a mixin:
.r(@i){
.r@{i} { opacity : ~r(0,1); }
}
.r(1);

compiler tells me 'Can not found r function in [object Object]'

so I guess the javascript evalution context inside a mixin is different with the context in root.

Will this be fixed in next version? or just add another way to pass a function inside mixin so I can release the power of javascript in less file?

@lukeapage
Copy link
Member

global.r or window.r ? browser or node?

@RocHo
Copy link
Author

RocHo commented Jan 23, 2013

@agatronic actual not browser and node, Im using WinLess a windows application to compile less file.

Is there any possible to setup a global function that every corner in less can access it?

@lukeapage
Copy link
Member

winless probably executes the browser version.. have you tried attaching to window instead of global?

@RocHo
Copy link
Author

RocHo commented Jan 23, 2013

@agatronic I print out the properties of this object , notice there are only global and GLOBAL property, so I think WinLess is using WSH as its runtime. I tried to set up an extend property to both this and global object, Under root context, all works well, function can be found. But in any mixin context, all properties disappeared, like the global object is entire new one.

@lukeapage
Copy link
Member

less.js evaluates in a new object...

https://github.com/cloudhead/less.js/blob/master/lib/less/tree/javascript.js#L19

I don't know much about wsh, but less doesn't define global - you should be able attach to a global object, I just don't know how to get this in wsh

http://stackoverflow.com/questions/9642491/getting-a-reference-to-the-global-object-in-an-unknown-environment-in-strict-mod

You might also want to look at #732 and ask for it to be re-instated

@RocHo
Copy link
Author

RocHo commented Jan 25, 2013

@agatronic Thanks for your help. I have know where the problem is.

First, WinLess is using node.js windows version, not WSH. So there is a global object.

Second, The reason function cannot accessed in a mixin is not because there are two different global context inside and outside mixin, It's because these functions are not define yet when needs them. I noticed that the javascript in mixins evaluated earlier than those are in root, So I changed function defination codes to:

.randomFunctions(){ .randomFunctions{ display:~``(function(s){ // define functions to global })(this)``; } } .randomFunctions();

All works well.

And thank you again.

@RocHo RocHo closed this as completed Jan 25, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants