JavaScript JavaScript closures and class like implementation javascript class closures

Code Snippet #1

The reference error occurs because of the following code: EchoMyName("Called by Person!");

EchoMyName is a self-invoked function located inside the scope of a JavaScript constructor named Person. Once it is self-invoked, it no longer exists. If we were in another programming language, we could make an analogy to an anonymous object.

In JavaScript, functions are actually objects. When you declare a function, you are creating an instance of a Function object.

Below are a few different ways of declaring a function.

//function statement
function x (a, b) { return a + b;  }

//function expression, this will store an anonymous function into a variable
var y = function (a, b) { return a + b;  };

//Function constructor 
var z = new Function ("a", "b", "return a + b;"); // avoid this form, it will prevent certain optimizations from the browser's JS engine because of the strings (this is just an example)

Code Snippet #2

When you declare a variable or a function inside a JavaScript function, it is limited to the scope of the function. The same is true for a constructor.

You would need to declare a function expression and store it inside a property of the Person constructor.

Little example below.

function Person(name) {
    this.Name = name;
    console.log("1 Person Fired!!");

    // notice that function is stored in this.EchoMyName
    this.EchoMyName = function(temp) {  
       console.log("2 EchoMyName - " + temp);
    };

    console.log("3 Still in Person!")
}

Person p = new Person("Bob");
p.EchoMyName("Hello World!");

output:

1 Person Fired!!
3 Still in Person!
2 EchoMyName - Hello World!

Prototypes

In JavaScript, there is no traditional inheritance like you would find in many other object oriented languages (C++, C#, Java). What you have instead is object chaining.

If we take your code in example, the Person constructor will have a shadowed object stored in a property named prototype. Each instance of Person will have a reference to the same prototype. Furthermore, the prototype of the prototype is Object.

I invite you to read the article Inheritance and the prototype chain from the Mozilla Developer Network for more information.

Answer:1
(function EchoMyName(temp) {
    console.log("EchoMyName - " + temp);
}("Self Called"));

This is a function expression, not a function definition; the name EchoMyName only refers to the function inside the function itself, so you can’t use it later. (Except in IE’s JScript, which is buggy.)

Your second problem stems from the fact that what you have isn’t a class; it’s a function. It’s possible to declare a function inside a function in JavaScript, and it will be scoped to that function, just like a regular variable (but hoisted so it can be used at any point in the function). A prototype is just an object, and doesn’t have access to the local variables of the constructor.

To sum up, in ECMAScript 5:

  • There are only three scopes:
    • Global
    • Function
    • catch
  • In a named function expression, the name is only accessible by that function
  • For a regular variable, there are no exceptions to the scope; functions on the prototype should use this if they need something of the target’s (and if they don’t, why are they on the prototype?)
  • Without the parentheses, a function expression looks like a function declaration, which acts like a variable in the same scope, but is “hoisted” so that no matter where it appears in the function, it acts as if it had appeared at the beginning
Answer:2

I have an html page with some inputs and textareas. I want them to have qTip with different texts. Here is my attempt First I add a qTip to every element, $('input, textarea').each(function() { ...

I have an html page with some inputs and textareas. I want them to have qTip with different texts. Here is my attempt First I add a qTip to every element, $('input, textarea').each(function() { ...

This is actually two question. I was playing around with the .eq method in jquery and decided to test it out using jsfiddle. What puzzles me is that when I supply an index that is obviously out of ...

This is actually two question. I was playing around with the .eq method in jquery and decided to test it out using jsfiddle. What puzzles me is that when I supply an index that is obviously out of ...

I try to implement a AngularJS router into my application. So I have define one route (in angular.module('MyModule').config()) : $routeProvider .when('/:stepId?', { templateUrl: EditorApp....

I try to implement a AngularJS router into my application. So I have define one route (in angular.module('MyModule').config()) : $routeProvider .when('/:stepId?', { templateUrl: EditorApp....

  1. angularjs route resolve redirect
  2. angularjs route resolve
  3. angularjs route resolve promise
  4. angularjs route resolve authentication
  5. angularjs route resolve example
  6. angularjs route resolve multiple promises
  7. angularjs route resolve inject service
  8. angularjs route resolve reject
  9. angularjs route resolve service
  10. angularjs route resolve not working
  11. angularjs route resolve parameters
  12. angularjs route resolve promise example
  13. angularjs route resolve property
  14. angularjs route resolve error
  15. angularjs route resolve wait
  16. angularjs route config resolve
  17. angularjs route state resolve

I am using JsPDF and want to create pdf from content inside a Section tag. i have tryed alot of guides but noone seam to work, and due to lack of demo code, i see no other alternative than try my luck ...

I am using JsPDF and want to create pdf from content inside a Section tag. i have tryed alot of guides but noone seam to work, and due to lack of demo code, i see no other alternative than try my luck ...

  1. jspdf creates empty pdf
  2. jspdf empty pdf