Efficiency can be inefficient

Efficiency can be inefficient

Sometimes, on the course of coding, we do what seems like a harmless change. A change rooted in a desire to reduce our variable use, or put as much onto one line and "look cool".

Perhaps you've run into this problem. Hopefully this will give someone some help. "Help, I'm trying to remove a marker from google maps and it says "setMap" is not a function". Or really any function that a marker would have in google map.

Let me tell you a tale of the errant delete key.

In Google's map documentation, you will see creation code for markers like this:

  marker = new google.maps.Marker({
    map: map,
    draggable: true,
    animation: google.maps.Animation.DROP,
    position: {lat: 59.327, lng: 18.067}
  })
  marker.addListener('click', toggleBounce);

Remembering chaining (most likely in jQuery), you remember that you can often take those uses of marker and condense them, like this:

  marker = new google.maps.Marker({
    map: map,
    draggable: true,
    animation: google.maps.Animation.DROP,
    position: {lat: 59.327, lng: 18.067}
  }).addListener('click', toggleBounce);

Seems kosher. After all, it was working on a marker in both cases. And in fact, you are right, it is working on a marker in both cases.

However, the variable marker, in this case, won't have an instance of Goole Map Marker, it will have an instance of Google Map Marker Events (or something to that effect).

The .addListener method does not return a google Marker. So the marker variable would get something other than a marker.

Which mean you will not be able to execute any marker methods on it.

Solution: don't delete the marker from the addListener. Let it run independently. Chaining only works when you return the same object. For example, here:

In an object, you can get away with this:

var t = new Test(10);
var a = t.double().double().add2().halve()

If every method that you are chaining returns the original object. Or the method you are invoking will be on the returned object. That means crafting your methods like this:

  halve(){
    this.val /2;
    return this;
  }
  add2(){
    this.val += 2;
    return this;
  }

Notice how every method returns the original object so it can be used in successive function calls.

You could do something like this from the example code:

var a = t.double().double().add2().halve().get().toFixed(2)

Notice how I am doing toFixed at the end of the chain. "get" returns a number, and toFixed is a method of a Number.

Hope it helps!


To view or add a comment, sign in

More articles by Daniel Paschal

  • curiouser and curiouser: a tale of css pseudo-elements

    A programming language is a set of rules. One of our jobs, as developers, is to understand those rules in order to make…

  • Google SEO: structured data

    SEO, or search-engine-optimization, for a long time was a game of figuring out what the search engines were looking for…

    2 Comments
  • CSS: font-display

    Custom Fonts on pages can be a cause of consternation when they don't load quickly. In the past, tricks usually…

  • css: font-face multi-source

    File this one under "that's cool, but probably not that useful" When specifying a font-face, you would normally specify…

  • More physical products meeting end of life due to support changes

    There have been a recent spate of product brickings that you should be familiar with. In this day and age, it is quite…

    1 Comment
  • Charter/Spectrum's security shutdown hints of interoperability problems to come

    It's not uncommon for companies, as they do mergers or shut down or simply determine a product isn't performing well…

  • Git 2.25.0 has landed

    Release notes here Some love for sparse-checkout (checking out a more skeleton version of a project) git log formatting…

  • My first experiences with AWS Lambda: a guide to trying node.js lambda

    Prologue I am first and foremost an monolithic person. I love starting up my own full servers.

  • History of Javascript

    Mocha, I mean Livescript, I mean Javascript, I mean ECMAscript has been around in some form since 1991. It has a very…

    2 Comments
  • nth-child/nth-last-child tricks

    CSS is rapidly on its way to being a very robust 'language'. There are many selectors that people are unaware of that…

    2 Comments

Others also viewed

Explore content categories