jQuery type function as a variable - javascript

can I create a variable that is a function of this type:
jQuery.fn.appendValueToTextArea = function(txt){
return this.each(function(){
this.value += txt;
});
};
?
I tried putting a var in front of it but I get a error.

If you want to call a method on an element like you wrote in the comment, you definitely have to extend jQuery.func.
Maybe you just want to store the name in a variable?
var name = 'appendValueToTextArea';
element[name]('text');
The only other way is to store a reference to the function and use .call() or .apply():
var func = jQuery.fn.appendValueToTextArea = function(txt) {
// ...
};
func.call(element, 'text');
Or (as I am not sure what you really want) you can assign the function to a variable first and then assign it to jQuery.fn.appendValueToTextArea:
var func = function(txt) {
// ...
};
jQuery.fn.appendValueToTextArea = func;

What happens if you define the variable after the function has been defined?
i.e.
jQuery.fn.appendValueToTextArea = function(txt){
return this.each(function(){
this.value += txt;
});
};
var fn = jQuery.fn.appendValueToTextArea;

jQuery.fn.appendValueToTextArea = function(txt){
return this.each(function(){
this.value += txt;
});
};
//This would put the function above in a variable.
//However, I'm not exactly sure what this gains you
var myfunction = jQuery.fn.appendValueToTextArea;

Related

How to pass the returned type as parameter to a new function (as if the function were be inside the return type)? js

Let's say that a hany any object, that return a string for example.
And I have the following:
function alterString(str){
return `${str} was altered!`
}
//I want call my method as if it were be inside the return type
let new_string = object.returnString().alterString();
I've already did this in C#, but I don't even know the name to research
String.prototype.alterString=function(){
return `${this} was altered!`;
}
var obj = {
returnString : function(){ return 'Hi'; }
};
let new_string = obj.returnString().alterString();
Or else you can add alterString() directly into returnString() as below.
var obj = {
returnString : function(){
this.text = "Hey";
}
};
obj.returnString.prototype.alterString = function(){
return `${this.text} was altered!`;
}
let new_string = new obj.returnString().alterString();

JavaScript function to edit original object

Is there any way, in JavaScript, to write a function like:
var a = "Hello"
function change(variable, value) {
//Code that edits original variable, not the variable argument
}
alert(a)
change(a, "World!");
alert(a);
And this would output first "Hello", and then "World!". Is there any way to write a function like that?
No, but a close alternative is to treat a as a JS object:
var a = { value : "Hello" };
function change(variable, value) {
//I'll let you work this part out
}
alert(a.value);
change(a, "World!");
alert(a.value);
Here is how you do that by using JavaScript Closures;
var obj = (function() {
var x = 'hello';
return {
get: function() {
return x;
},
set: function(v) {
x = v; }
}
})();
obj.get(); // hello
obj.set('world');
obj.get(); // world
You can only the change the value of the variable using the get and set functions.

calling a string as function call in javascript

What is wrong in this ?? I want to call a string as a function. Could someone please help me on this
var wnameSpace = (function(){
var privateVar = '';
privateVar = "dummyFunction";
dummyFunction = function(){
console.log("hurray dummyFunction called here");
};
return {
publicFunction:function() {
console.log(window[wnameSpace])
}
}
})();
wnameSpace.publicFunction();
Well, you're accessing window[wnameSpace] but I think you meant to use window[privateVar]. Also, you probably meant to invoke the function rather than just log it. Try this:
var wnameSpace = (function(){
var privateVar = "dummyFunction";
dummyFunction = function(){
console.log("hurray dummyFunction called here");
};
return {
publicFunction:function() {
window[privateVar](); // "hurray dummyFunction called here"
}
}
})();
wnameSpace.publicFunction();
However, I wouldn't recommend using this kind of code in production. It's hard to tell what you want, exactly, but I'm pretty sure there is a better way to accomplish it.
var wnameSpace = (function(){
var privateVar = '';
privateVar = "dummyFunction";
dummyFunction = function(){
console.log("hurray dummyFunction called here");
};
return {
publicFunction:function(){
var fn = window[privateVar]; if (typeof fn === "function") fn();
}
}
})();
wnameSpace.publicFunction();
Will also work.. Thank you for all the anwsers

Javascript oop undefined value: Method to add a scope?

Code:
var test = {
con: true
};
var conrun= function(){
return this.con;
};
Function.prototype.curry = function(scope){
var fn = this;
var scope = scope||window;
return function(){
fn.apply(scope,arguments);
}
}
conrun = conrun.curry(test);
alert(conrun());
//result:undefined
"curry" method, the function will return, "conrun" fonkiyonuna "test" add to the scope of...
What should I do ?
Your curry loses the return value. Change that line to:
return fn.apply(scope, arguments);

Javascript dynamically invoke object method from string

Can I dynamically call an object method having the method name as a string? I would imagine it like this:
var FooClass = function() {
this.smile = function() {};
}
var method = "smile";
var foo = new FooClass();
// I want to run smile on the foo instance.
foo.{mysterious code}(); // being executed as foo.smile();
if the name of the property is stored in a variable, use []
foo[method]();
Properties of objects can be accessed through the array notation:
var method = "smile";
foo[method](); // will execute the method "smile"
When we call a function inside an object, we need provide the name of the function as a String.
var obj = {talk: function(){ console.log('Hi') }};
obj['talk'](); //prints "Hi"
obj[talk]()// Does not work
method can be call with eval
eval("foo." + method + "()");
might not be very good way.
I would like to leave an example here for this. For example; i want to call a dynamically check method while submitting the form.
<form data-before-submit="MyObject.myMethod">
<button type="submit">Submit</button>
</form>
$('form').on('submit', function(e){
var beforeSubmit = $(this).attr('data-before-submit');
if( beforeSubmit ){
params = beforeSubmit.split(".");
objectName = params[0];
methodName = params[1];
result = window[objectName][methodName]($(this));
if( result !== true ){
e.preventDefault();
}
}
});
var MyObject = {
myMethod = function(form){
console.log('worked');
return true;
}
};

Categories

Resources