Coder Social home page Coder Social logo

wdi_7_js_demo_adv_functions's Introduction

General Assembly Logo

Callbacks and the "this" pointer.

Learn some important concepts about how javascript functions behave at Runtime.

Objectives

  • Understand how to use functions as first-class objects.
  • Using function callbacks to increase efficiency.
  • Using the javascript functions, (call, apply and bind), to change a function's execution/runtime "Runtime Context" with 'this' pointer.
  • Learning how the 'this' pointer changes at Runtime, execution, time.
  • Know that Scope is NOT the same as the 'this' pointer. They are two separate concepts. (There is another lesson on scope).
    • Scope is lexical scope that is constructed during javascript compilation.
    • It's static, never changes at Runtime, when javascript code is executing.
    • All you need to know to determine scope can be seen from reading the code. You don't have to think about what happens when the code is running to determine a variable's scope.
  • 'this' is Runtime Context. A function get's it 'this' pointer from something that called the function at Runtime.

Callbacks

In JavaScript, functions are first-class objects; that is, functions are of the type Object and they can be used in a first-class manner like any other object (String, Array, Number, etc.) since they are in fact objects themselves.From Javascript Callbacks and how to use them

They can be “stored in variables, passed as arguments to functions, created within functions, and returned from functions”. From Javascript Callbacks and how to use them

Lab

Work through this article Javascript Callbacks and how to use them.

  • Create a branch name <initials>_done, e.g. Thomas George Dyer branch would be named tgd_done.
git checkout -b <initials>_done
  • Create a solutions directory for your javascript files.

  • Create javascript files, in solutions dir, for each example.

    I created 8 javascript files and ran a couple in the browser, a couple using node.

    You may need to create an associated HTML file with DOM elements refered to in exercises.

  • Step thru the code in a browser debugger and create comments at each point that may be surprising, or that need clarification.

    Some examples, (Multiple Callback Functions Allowed, Callback Hell), cannot be coded. Write in your own words what the point of these examples are.

  • How can creating callbacks help to:

    • DRY up your code?
    • Implement better abstractions?
    • Make your code more maintainable?
    • Help to reuse code?

Put all your answers in callback_benefits.txt file. And submit a pull request.

Demo

In the index.html change the script at the end of the body to point to one of these javascript files.

  • simple_iter.js - Simple DOM iteration.
  • simple_callback.js - Simple DOM iteration with callback.
  • anon_callback.js - Simple DOM iteration with anonymouscallback.
  • problem_callback.js - Simple DOM iteration with callback that is an object's method. How 'this' is used in callbacks.

"this"" pointer and a Function's Runtime Context.

"We use this similar to the way we use pronouns in natural languages like English and French. We write: “John is running fast because he is trying to catch the train.” Note the use of the pronoun “he.” We could have written this: “John is running fast because John is trying to catch the train.” We don’t reuse “John” in this manner, for if we do, our family, friends, and colleagues would abandon us. Yes, they would. In a similar aesthetic manner, we use the this keyword as a shortcut, a referent to refer to an object." from Understanding Javascript 'this' pointer.

Lab

Read this. Understanding Javascript 'this' pointer.

Create files for each example. You may need to create an associated HTML file with DOM elements refered to in exercises.

Step thru the code in a browser debugger and create comments at each point that may be surprising, or that need clarification.

Demo

In the this_index.html change the script at the end of the body to point to one of these javascript files

  • this_object_literal.js - 'this' in an object literal.
  • this_global_function.js - 'this' in a global function.
  • this_event_handler_broken.js - Broken button event handler.
  • this_event_handler_fixed.js - Fixed button event handler.
  • this_inner_function_bad.js - Broken inner function.
  • this_inner_function_fixed.js - Fixed inner function.
  • this_reassign_method_bad.js - Changing the runtime context of a method to the global object.
  • this_reassign_method_fixed.js - Changing the runtime context of a method with the javscript bind function.

References

wdi_7_js_demo_adv_functions's People

Contributors

tdyer avatar fishermanswharff avatar

Watchers

James Cloos avatar Ross Degnen avatar Jason Weeks avatar Chris R. Payne avatar Christopher Austin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.