spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Add method to function


In this case you want to modify the line:

this.prototype[name] = func;

to just

this[name] = func;

Demo: http://jsfiddle.net/s8aed1bx/1/

Alternatively you could use

this.constructor.prototype[name] =
func;

but that would add each new method to every function, not just the one you call method on. Or you could use:

var f2 = new func();
f2.greet();

Why?

When looking up properties on an object in JavaScript, the object itself is searched, and then the prototype of the constructor of the object is searched (and so on up the chain). The .constructor of func is Function.

In this case what you have done would work if you wrote:

func.method('greet', function() {
  console.log("Greetings from greet()!");
});
var f2 = new func();
f2.greet();

The prototype of a function is used for new instances created by that function. (The objects you create have a .constructor of the creating function itself.)


Categories : Javascript

Related to : Add method to function
Calling a method of a function constructor from another function in the global scope - Javascript
Close. You need to call this.markDone(); this is set differently inside the forEach loop, so you need to set it explicitly or catch it earlier and set it to something else, like that Try this: function func1() { console.log("func 1"); this.markDone(); } And: this.execute = function() { var that = this; funcList.forEach(function(func){ func.apply(that); }); }

Categories : Javascript
Call function in other method
Your function testFunction() is calling function function getPrice( $type ) and function getPrice( $type ) is returning 0 so may be because of 0 amount you are getting this error from external api (Payment gateway api).

Categories : PHP
Method vs. Function invocation pattern
There is no difference between the person.calculateAge and person.greet as both will be added as a property to the 'person' object. However the author is trying to explain how the 'this' keyword is set during varying execution context which normally does not depend on where the function is defined, rather it depends on how a function is invoked. In both the cases above the 'this' keyword will be s

Categories : Javascript
Function variable not available when require page through another class method
i'll give you a simple example, from that you can understand accessing mechanism first.php <?php class first { private $name; function __construct() { $this->name="name"; } function changeName(){ $this->name="newName"; } function getName() { return $this->name; } } ?> second.php <?php include 'fir

Categories : PHP
Passing an instance method to an API that expects a C function pointer
You can't. MyClass::process_data can be thought of as a void(MyClass*, DataRecord*), which is the wrong type. You'd have to wrap your class pointer into the call somehow. One approach might be to introduce a type with a static pointer: struct MyClassCallbackHelper { static MyClass* myClass; static void callback(DataRecord* record) { myClass->process_data(record); } };

Categories : C++
Recently Add
Busy Indicator while page is loading
parent.document.getElementById and images path
Can not get bootstrap datetime picker to display properly
reading select box jquery issues
DC.JS Pie Chart + Select Deslect from external JavaScript function
Why would I ever need to cancelAnimationFrame()
Angular.ui alert doesn't close
HTML/JS How to position a rectangle relative to 'any' side of the parent canvas element
parse variable to xml
C# Dictionary equivalent in JavaScript
ExtJS Dashboard Panel Fit Panel Vertically
jQuery cross domain ajax call - Interpreted as script but transferred with MIME type text/xml
Reactjs map returns unexpected token }
Working with multiple pages in apache cordova and vs2013
Resize iframe to loaded content
Why 13596*0.1 is different than 13596/10?
AngularJS Validate all forms on application load
javascript open popup without menu/url/titlebar/scrollbar/anything
Can I use Razor syntax inside Javascript included in a ScriptBundle?
How to show a box for comment reply with id
parse name from user input when using jquery serialize
How to parse and load javascript object?
Marionette not navigating to the same url
Adding Google+ Sign in button to website
GRUNT - Install Grunt Packages?
how should I create the path of templateUrl property in Angular ui-router?
Update mysql database with jquery array using ajax
Run the same function on each item in a list / array
Get all ticks values (NVD3)
Button Click Event not Passing input String to MVC Controller Method
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.