how to trace in the name of its variable - actionscript-3

var abc:int=123
trace(abc)
//actual output:
123
//my expected output:
abc:123
Although I can type trace("abc:"+abc) by hands,but I still want to have a more simple way to trace
I have tried something like
function tracee(word){
trace("word:"+word)
}
function traceee(word){
var wordd:Srting=word
trace(wordd+word)
}
but these functions are not working.
is it possible to have the expected output?

import flash.utils.describeType;
var num:Number = 47;
function customTrace(word:*){
trace(describeType(this).variable.#name + " : "+word)
}
customTrace(num);
SOURCE

No. Variables are passed to functions by link (memory offset) or by value, so you don't have any data about names.
The one thing I can propose - is to use automatic code generation in IDEs. For example, in IntelliJ Idea it is in Settings -> Live Templates, that you can use via ctrl+J in editor.

Related

pass a local variable to an click function

I want to pass a local variable from one function to another and I have tried some solutions but they didn't work because I have a click function, I need to put the variable first of all and I don't how to do it, also I declared the variable outside the function but if I use it outside of all the functions it doesn't has all its values or inside the function resaltar nothing appears, any help is welcome
let children=$('div[class^="fila"], div[class^="pieceRow"]' ).children()
var clase
$(children).each(function getClass(){
clase=$(this).attr('class')
$(clase).on('click', function resaltar(){
if (clase==clase) {
$(this).addClass('shadow')
}
})
})
this is the html code https://jsfiddle.net/qb5fwcus/
Please try this code :
let children = $('div[class^="fila"], div[class^="pieceRow"]' ).children();
$(children).on('click', function(){
var clase = $(this).attr('class');
resaltar(clase);
})
function resaltar(clase){
$('.shadow').removeClass('shadow');
$('.' + clase).addClass('shadow');
}
Explanation : You can not pass any value for the callback function for any event handler. Either it can be an anonymous function, or a function, not requiring any argument. However, you can achieve that, by making the callback function anonymous, and call any function from it. In this way, you can pass variables.
PS : Let me know if I got it wrong in any manner :)
Let's assume that you will be passing it to a pure JS function.
function myFunc() {
console.log("My function!");
}
In your 'click', you're calling the function ''resalter'', that you're also defining on the spot.
You want to call myFunc, so:
$(clase).on('click', myFunc())
Now, myFunc is not expecting a variable. Let's just pass a variable:
function myFunc(myVar) {
console.log("Passing a variable of type: " + typeof myVar);
}
Now, you're only expected to pass this var in the function you're calling. Given the previous example I gave, we have:
let x = 1; // our variable
$(clase).on('click', myFunc(x))
This way you're passing 'x' as a variable, of type integer. Use this code as inspiration to try and reach your goal. It is a bit hard to give a more exact answer, given that we don't know what variables have to be passed to what function and what the purpose is.
Good luck!

Error with custom Search and Replace function for Google Sites

I'm trying to use a script to replace a particular string with a different string. I think the code is right, but I keep getting the error "Object does not allow properties to be added or changed."
Does anyone know what could be going wrong?
function searchAndReplace() {
var teams = SitesApp.getPageByUrl("https://sites.google.com/a/directory/teams");
var list = teams.getChildren();
list.forEach(function(element){
page = element.getChildren();
});
page.forEach(function(element) {
var html = element.getHtmlContent();
html.replace(/foo/, 'bar');
element.setHtmlContent = html;
});
};
Try This:
Javascript reference:
The replace() method returns a new string with some or all matches of a pattern replaced by a replacement.
I think the issue here is that forEach cannot change the array that it is called upon. From developer.mozilla.org "forEach() does not mutate the array on which it is called (although callback, if invoked, may do so)."
Try doing it with a regular loop.

Wikimedia function to get all my Templates

I need to get all the pages I have created like Templates in my wikimedia webpage. I have to do this with javascript.
Is this possible?
You can do this with a UserContribs API query, like this:
https://en.wikipedia.org/w/api.php?format=jsonfm&action=query&list=usercontribs&ucuser=Ilmari_Karonen&ucnamespace=10&ucshow=new&continue=
Basically, the parameters you need are:
format=json to get results in JSON format, which is probably what you want for JavaScript. (I've used jsonfm in the example link above to get pretty-printed human readable output.)
action=query to indicate that this is, indeed, a query rather than, say, an edit or a login attempt.
list=usercontribs to indicate that you want a list of a user's contributions (i.e. the stuff you see on the Special:Contributions page).
ucuser=your_username to select which user's contributions you want to see. (The example link above shows mine.)
ucnamespace=10 to select only contributions to templates. (10 is the namespace number for the built-in Template namespace).
ucshow=new to select only contributions that involve creating a new page. (Note that this also includes page moves; I don't see any simple way to filter those out.)
Of course, there are other parameters you may also want to include.
I've also included an empty continue= parameter to indicate that I want to use the new query continuation syntax, and to suppress the warning about it. Obviously, if you actually want to use query continuation, you'll need to implement the client-side part yourself (or use an MW API client that implements it for you). Here's one simplistic way to do that:
function getNewTemplatesForUser( username ) {
var queryURL = 'https://en.wikipedia.org/w/api.php?format=json&action=query&list=usercontribs&ucnamespace=10&ucshow=new';
queryURL += '&ucuser=' + encodeURIComponent( username );
var callback = function( json ) {
// TODO: actually process the results here
if ( json.continue ) {
var continueURL = queryURL;
for ( var attr in json.continue ) {
continueURL += '&' + attr + '=' + encodeURIComponent( json.continue[attr] );
}
doAjaxRequest( continueURL, callback );
}
};
doAjaxRequest( queryURL + '&continue=', callback );
}

Unknown macro in library

I am trying to make a library, went well so far, but after adding a few functions it went bad.
When I run the script form the editor it is written in the script works. But when I try to test it the script cannot recognize the server handlers, giving an error: Unknown macro handler_function_name
I checked, all the names in the handlers correspond to names of functions. I read that some people had problems because the code was in different files, moved all the code in the same file the problem is still there.
It does not behave like that for all the handlers...
What else could be the reason for this?
edit:
The app creates additional panels during as a response to "clicks". Handlers of elements on those panels are the ones who's macros (that is handler functions) the app is not able to "find".
How can this be solved?
(except for the solution to put all the panels in the original panel and then change visibility, this works as far as handlers go but raises other problems)
So to put some code here, this is very very simple code...
function notWorkingGUI(){
var app=UiApp.createApplication();
var appPanel=app.createVerticalPanel().setId("appPanel");
var handler1=app.createServerHandler("handlerFunction1").addCallbackElement(appPanel);
var firstButton=app.createButton("Button 1", handler1);
appPanel.add(firstButton);
app.add(appPanel);
SpreadsheetApp.getActive().show(app);
}
function handlerFunction1(e){
var app=UiApp.getActiveApplication();
var appPanel2=app.createVerticalPanel().setId("appPanel2").setStyleAttribute("zIndex", 0).setStyleAttribute("position", "fixed");
var handler2=app.createServerHandler("handlerFunction2").addCallbackElement(appPanel2);
var secondButton=app.createButton("Button 2", handler2);
var label=app.createLabel("This should get visible after the click").setId("label").setVisible(false);
appPanel2.add(secondButton).add(label);
app.add(appPanel2);
return app;
}
function handlerFunction2(e){
var app=UiApp.getActiveApplication();
app.getElementById("label").setVisible(true);
return app;
}
This will work as expected when executed from the editor in which it is written, that is it will show firstButton then secondButton and finaly the label, however if it would be published as a library and invoked from an other script it would only recognise functionHandler1, that is show firstButton, secondButton but after a click on the secondButton an error message will be seen.
However if the script would be written like this:
function workingGUI(){
//previous first part
var app=UiApp.createApplication();
var appPanel=app.createVerticalPanel().setId("appPanel");
var handler1=app.createServerHandler("handlerFunction1a").addCallbackElement(appPanel);
var firstButton=app.createButton("Button 1", handler1);
//previous second part
var appPanel2=app.createVerticalPanel().setId("appPanel2").setStyleAttribute("zIndex", 0).setStyleAttribute("position", "fixed");
var handler2=app.createServerHandler("handlerFunction2a").addCallbackElement(appPanel2);
var secondButton=app.createButton("Button 2", handler2).setId("button2");
appPanel.add(firstButton);
app.add(appPanel);
SpreadsheetApp.getActive().show(app);
}
function handlerFunction1a(e){
var app=UiApp.getActiveApplication();
var label=app.createLabel("This should get visible after the click").setId("label").setVisible(false);
app.getElementById("appPanel2").add(app.getElementById("button2")).add(label);
app.add(app.getElementById("appPanel2"));
return app;
}
function handlerFunction2a(e){
var app=UiApp.getActiveApplication();
app.getElementById("label").setVisible(true);
return app;
}
Note that all handlers must be defined in the main function, meaning that also all the elements using those handlers and all the callback elements have to be defined here.
Then it would work even as a library, however for some reason this makes the script much much slower even for such a simple example.
The issue is here:
http://code.google.com/p/google-apps-script-issues/issues/detail?id=1346
It is calling the local code rather than the library code.
I wonder if it is still slow if you add a stub function in the local code?
i.e.
function runthis() {
library.createGUI();
}
function myevent() {
library.myevent();
}
I worked around this problem, it makes the script a bit slower but if you define all the handlers (that implies all the UI elements) in the original function it will work.

'Global' object in node.js

I am using 0.3.1-pre Node.js
Doing this:
typeof global.parseInt
results in
'undefined'
However when pressing [Tab] in the console after typing 'global.' gives a list of functions, including parseInt.
So is parseInt a member of the global namespace or not?
As of NodeJS v0.8.14 global seems to work across modules like the window object does in the browser.
Test:
a.js:
a1 = console.log; // Will be accessed from b.js
global.a2 = console.log; // Will be accessed from b.js
require('./b.js');
b1('a: b1');
b2('a: b2');
global.b1('a: global.b1');
global.b2('a: global.b2');
b.js:
a1('b: a1');
a2('b: a2');
global.a1('b: global.a1');
global.a2('b: global.a2');
b1 = console.log; // Will be accessed from a.js
global.b2 = console.log; // Will be accessed from a.js
Running a.js outputs:
b: a1
b: a2
b: global.a1
b: global.a2
a: b1
a: b2
a: global.b1
a: global.b2
Apparently, the global object isn't the global object as window is in the browser. It's (according to micheil in #nodejs # freenode) really only used internally. Something about global closures and whatnot.
parseInt and setTimeout and all those buddies are globals on their own. Not part of any visible global object.
Defining variable in app.js without var, just like myvar='someval' makes it visible inside every .js in your project
FAILS:
if( global[ some_object_i_want_to_exist ] ){ ... }
WORKS:
//: outside of all functions, including IIFE.
const THE_GLOBAL_YOU_PROBABLY_WANT_IS_THIS=( this );
//: Within a function:
const G = THE_GLOBAL_YOU_PROBABLY_WANT_IS_THIS;
if( G[ some_object_i_want_to_exist ] ){ ... }
I am assuming you got to this page about "global" in node.js because you wanted the equivalent of "window" in order to check for globally declared variables. bFunc's solution didn't work for me, as it seems to require that one explicitly does something like:
global.some_object_i_want_to_exist = whatever;
as a pre-requisit to using
global[ some_object_i_want_to_exist ]
EDIT: Looking at my code it seems that the only reason my solution worked is because I used "exports.some_object_i_want_to_exist" somewhere else in the
file. Without that, my solution fails. So... I have no clue how to reliable determine if an object exists in a given scope in Node.js.
Here is the documentation on global object:
https://nodejs.org/api/globals.html
I am going to leave my answer here because I hear people are more likely to correct you when you are wrong, so maybe someone will correct me with the answer to the problem.