+2 votes
by (100 points)
function setupSomeGlobals() {
    // Local variable that ends up within closure

    var num = 666;
    // Store some references to functions as global variables
    gAlertNumber = function() {console.log(1); alert(num); }
    gIncreaseNumber = function() { num++; }
    gSetNumber = function(x) { num = x; }
}

how can I accesss here gAlertNumber method?

update: this code is the Example 4 in an answer on How do JavaScript closures work?

10 Answers

+21 votes
by (100 points)

Assuming you're in a web browser, you have to execute setupSomeGlobal() first. Then your non-declared handler variables g... will be created under the global object window and you'll be able to execute gAlertNumber() from anywhere in your page.

You could execute setupSomeGlobal() in the body's onload :

<html>
    <head>
        <script>
            function setupSomeGlobals() {
                // Local variable that ends up within closure

                var num = 666;
                // Store some references to functions as global variables
                gAlertNumber = function() {console.log(1); alert(num); }
                gIncreaseNumber = function() { num++; }
                gSetNumber = function(x) { num = x; }
            }
        </script>
    </head>

    <body onload="setupSomeGlobals();">
        <input type="button" value="Show me more or less the number of the beast" onclick="gAlertNumber();"
    </body>
</html>

That said, your method of setting up "global" functions isn't very pretty. I quite like the pattern described here for example.

+15 votes
by (100 points)

Here is an example,

(function() {
   console.log(1);
   // Local variable that ends up within closure
   var num = 666;
   var sayAlert = function() { console.log(num); }
   num++;
   return sayAlert();
})();

This will call immediately after definition.

So with your code,

function setupSomeGlobals() {

  var num = 666;
  // Store some references to functions as global variables
  gAlertNumber = function() {console.log(1); alert(num); }
  gIncreaseNumber = function() { num++; }
  gSetNumber = function(x) { num = x; }

  gAlertNumber();

}
setupSomeGlobals();

Here you can call the child function gAlertNumber() inside your parent function setupSomeGlobals() and you cannot access it outside the parent function.

But you can call this after calling parent function, that means don't call the gAlertNumber() inside parent function. call it after calling parent like,

function setupSomeGlobals() {
    // Local variable that ends up within closure
    var num = 666;
    // Store some references to functions as global variables
    gAlertNumber = function() {console.log(1); alert(num); }
    gIncreaseNumber = function() { num++; }
    gSetNumber = function(x) { num = x; }
}

setupSomeGlobals();
gAlertNumber();
+12 votes
by (100 points)

Return an object from setSomeGlobals() that contains the three methods. Through this object you will be able to access the functions of interest and manipulate num and keep its state, but you will not be able to access num directly. This is known as the module pattern, an application of closure.

+11 votes
by (100 points)

Well this will work in browser

gAlertNumber is considered being window property.. It would be the same as calling

  window.gAlertNumber()

so inside your setSomeGlobals you assign function object to the undefined window property. Than you close the local variable num inside that object which is already created inside window object. Thus you can access it from window scope.

+7 votes
by (100 points)

Try like this

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    android:id="@+id/relativeinfo1">

    <View
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:layout_above="@+id/titleinfo"
        android:layout_toRightOf="@+id/imageinfo"/>


    <TextView
        android:id="@+id/titleinfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:textSize="20dp"
        android:textStyle="bold"
        android:textColor="#000000"
        android:layout_toRightOf="@+id/imageinfo"
        android:layout_marginLeft="20dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="phone"
        android:id="@+id/mooobil"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="phone"
        android:id="@+id/telefoneeo"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="email"
        android:id="@+id/emailese"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="web"
        android:id="@+id/honlapese"/>


    <ImageView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:id="@+id/imageinfo"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"/>

    <View
        android:layout_width="wrap_content"
        android:layout_height="15dp"
        android:id="@+id/view1"
        android:layout_below="@+id/imageinfo"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cimtext"
        android:textStyle="bold"
        android:text="Cím: "
        android:textSize="15dp"
        android:textColor="#000000"
        android:visibility="invisible"/>


    <View
        android:layout_width="wrap_content"
        android:layout_height="10dp"
        android:id="@+id/vonal"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="#FFD700" />

    <View
        android:layout_width="wrap_content"
        android:layout_height="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:id="@+id/vonal2"
        android:background="#FFD700"/>


    <RelativeLayout
        android:layout_alignParentBottom="true"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context=".MainActivity"
        android:id="@+id/relativemaplayout"
        android:clickable="true"
        android:background="#000000">

        <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            class="com.google.android.gms.maps.MapFragment" />


    </RelativeLayout>
</RelativeLayout>

update

if still you have problem please refer this and try

Google Maps API v2 SupportMapFragment inside ScrollView - users cannot scroll the map vertically

+5 votes
by (100 points)

"Like" button is not visible initially, so you cannot just click on it- you should make if visible first, so try following code:

number = 0
driver.execute_script('document.querySelectorAll("button.btn.btn-default.pdt-card-like")[number].style.display="block";')
driver.execute_script('document.querySelectorAll("button.btn.btn-default.pdt-card-like")[number].style.visibility="visible";')
driver.find_elements_by_xpath('//button[@class="btn btn-default pdt-card-like"]')[number].click()

This should allow you to "like" first item. Set another number value to change target item

+1 vote
by (100 points)

You can add a BasicAuthenticationFilter to the security filter chain to get OAuth2 OR Basic authentication security on a protected resource. Example config is below...

@Configuration
@EnableResourceServer
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManagerBean;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        final String[] userEndpoints = {
            "/v1/api/airline"
        };

        final String[] adminEndpoints = {
                "/v1/api/jobs**"
            };

        http
            .requestMatchers()
                .antMatchers(userEndpoints)
                .antMatchers(adminEndpoints)
                .antMatchers("/secure/**")
                .and()
            .authorizeRequests()
                .antMatchers("/secure/**").authenticated()
                .antMatchers(userEndpoints).hasRole("USER")
                .antMatchers(adminEndpoints).hasRole("ADMIN");

        // @formatter:on
        http.addFilterBefore(new BasicAuthenticationFilter(authenticationManagerBean),
                UsernamePasswordAuthenticationFilter.class);
    }

}
0 votes
by (100 points)

Response gives you the numerical result while class gives you the label assigned to that value.

Response lets you to determine your threshold. For instance,

glm.fit = glm(Direction~., data=data, family = binomial, subset = train)
glm.probs = predict(glm.fit, test, type = "response")

In glm.probs we have some numerical values between 0 and 1. Now we can determine the threshold value, let's say 0.6. Direction has two possible outcomes, up or down.

glm.pred = rep("Down",length(test))
glm.pred[glm.probs>.6] = "Up"
0 votes
by (100 points)

You need to get the prices from the dd elements with the class="price" :

$('dl', '#col2').each(function() {
    var price = $('dd.price', this).text();
    prices.push(price);
});
0 votes
by (100 points)

Think about commit histories first, last, and all but only. Everything in git reduces to commit names and their ancestries. Every refname you see, the very first thing you do is translate it to a commit id, because that's what git does. Everything in git is stored in the object db. "The staging area" doesn't really exist, it's a notional status for things in the object db (things "git add"ed to it, or fetched into it, or pushed into it, at some point) and the index. They might not yet be referenced by an actual commit. No matter. Don't get hung up on abstractions, git is concrete. Learn what the operations are actually doing, the point of them isn't to implement some abstraction.

...