This question already has answers here:
Can you alter a Javascript function after declaring it?
(12 answers)
Closed 6 years ago.
In our ordering system there is an embedded function I have no access to at all. Conveniently there is a spelling error in it so when a user clicks something a popup appears and has grammar issues in it.
Is there a way for me to replace that text or replace the function with a new function that has all the same code but correct spelling?
This is the function I need to edit.. note confirm says 'You selection' not 'Your selection'
I'd prefer to replace the whole thing because I may want to do some other edits but for now I'd like to fix that spelling error.
function showProof()
{
var blnSubmit = false;
var strHid='';
var strSave='';
if (aryTemplateChoices.length == 0)
{
blnSubmit = true;
}
else
{
for (var i=1; i<=aryTemplateChoices.length; i++)
{
strSave = aryTemplateChoices[i-1];
if (strSave=='')strSave='0';
if (document.getElementById('hidctrl'+ i))strHid = document.getElementById('hidctrl'+ i).value;
if (strHid=='')strHid='0';
if ((strSave != '0') && (strHid != strSave))
{
blnSubmit = true;
break;
}
}
}
if (blnSubmit)
{
if (confirm('Your selection has changed, do you want to save?'))
{
document.getElementById('subtype').value = 'proof';
document.getElementById('prevclick').value = '';
document.getElementById('frm1').submit();
}
}
canAddToCart();
//<!--WRITE-->
getQuantityPrice()
//<!--/WRITE-->
loadProof();
}
It doesn't really matter where the original function is and how you access it, as long as you just redefine the function (with the same name and corrected code) at a scope closer to where you want to use it.
Here's an example:
function foo(){
console.log("ORIGINAL foo says hello.");
}
function foo(){
console.log("NEW foo says hello.");
}
// The second declaration is in the same scope as the first, but it comes after the first
// so it overwrites that declaration and the second one is the one that is used.
foo();
Related
This question already has answers here:
How to access the correct `this` inside a callback
(13 answers)
Closed 3 years ago.
I will be the first to admit, I don't always get JS Promises. So if this is a stupid question I apologize in advance. =) Given the code below, I need to set a property in the class that contains this function (i.e. I need to call "this" within the fullfullment methods and have it refer back to the class.
I saw a few things that related to setting context, but they also required closures (()=>{...code..}) which don't work so well in Internet Explorer. And we can hate on that browser all we want but at the end of the day its a requirement that this code works in IE.
So my question today is, how do I get a reference to this passed into the methods below?
var result = this.saveChanges();
return Promise.resolve(result).then(function (value) {
return value.HasError === false;
}, function (value) {
if (value && value.responseJSON) {
return value.responseJSON.HasError === false;
}
return false;
});
Your assistance is appreciated immeasurably.
You can declare a variable that references this and use that in the function.
var self = this;
var result = this.saveChanges();
return Promise.resolve(result).then(function (value) {
self.someProperty = true;
return value.HasError === false;
}, function (value) {
if (value && value.responseJSON) {
return value.responseJSON.HasError === false;
}
return false;
});
This question already has answers here:
Return all of the functions that are defined in a Javascript file
(4 answers)
Closed 3 years ago.
I have a javascript file and in that there are some functions defined in it.
How can i get all the function name defined in that particular js file in an array of string
function getabc1() {
//block of code
}
function getabc2() {
//block of code
}
function getabc3() {
//block of code
}
function getabc4() {
//block of code
}
function getabc5() {
//block of code
}
function getabc6() {
//block of code
}
function getabc7() {
//block of code
}
I need all the 7 functions defined in this particular js file.
Only name of function
It will be great help if someone can help me on this.
You're just gonna inspect the code a little bit, answers in Stackoverflow aren't always meant to give you the exact thing you need. You should learn to derive from it. Looking at the link #AndroidNoobie said, all you had to do is push the key instead of the entire iterated value.
As stated on https://stackoverflow.com/a/11279959/7325182:
Declare it in a pseudo namespace
var MyNamespace = function(){
function getAllFunctions(){
var myfunctions = [];
for (var l in this){
if (this.hasOwnProperty(l) &&
this[l] instanceof Function &&
!/myfunctions/i.test(l)){
myfunctions.push(l);
}
}
return myfunctions;
}
function foo(){
//method body goes here
}
function bar(){
//method body goes here
}
function baz(){
//method body goes here
}
return { getAllFunctions: getAllFunctions
,foo: foo
,bar: bar
,baz: baz };
}();
//usage
var allfns = MyNamespace.getAllFunctions();
console.log(allfns)
//=> allfns is now an array of functions.
// You can run allfns[0]() for example
This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 6 years ago.
I can't seem to be able to set this.chkOK inside my ajax function. I'm sorta clueless how I go about doing this so I thought maybe calling validateFields.call(this) should of fixed my problem but I found out that wasn't the case. So I'm sorta lost what to do for my next step. I don't want to set this to a global variable unless I have to. I'm trying to set this.chkOK = true
function validateFields() {
this.chkOK = null;
this.username = function() {
if(FS.gID('username').value.length >= 2) {
var user = FS.gID('username').value;
//Make sure that the username doesn't already exist
FS.ajax('/server/chkUser.php?user='+user,'GET',function(){
validateFields.call(this);
if(xmlText == 0) {
this.chkOK = true;
alert("This user doesn't exist.");
}
else if(xmlText == 1) {
alert("Theres already a user with this username");
this.chkOK = false;
}
});
}
else {
alert("empty");
this.chkOK = false;
}
alert(this.chkOK);
}
}
The value of this in your example is NOT the function inside which it's declared, like you assume in your code.
If you simply use var chkOK = null; instead of this.chkOK = null; it should start to work.
It is because this inside the FS.ajax is not the same with this you intended to work with. this in FS.ajax means this of FS
You may assign this into another variable and use it inside FS.ajax. For example,
Note: Unless you know the reason why you put this.chkOk inside the function (such as expected validateFields be invoked by call or
apply) this is global object (which you don't want to) or
undefined in strict mode which will cause the code to fail
function validateFields() {
this.chkOK = null;
// ** assign this to that. So you can reference it inside FS.ajax **
var that = this;
this.username = function() {
if(FS.gID('username').value.length >= 2) {
var user = FS.gID('username').value;
//Make sure that the username doesn't already exist
FS.ajax('/server/chkUser.php?user='+user,'GET',function(){
validateFields.call(this);
if(xmlText == 0) {
that.chkOK = true; // use `that` instead of `this`.
alert("This user doesn't exist.");
} else if(xmlText == 1) {
alert("Theres already a user with this username");
that.chkOK = false; // use `that` instead of `this`
}
});
} else {
alert("empty");
this.chkOK = false;
}
alert(this.chkOK);
}
}
This question already has answers here:
Different ways to execute IIFE?
(2 answers)
Closed 8 years ago.
I've been using the following to begin filling out code for an IIFE:
(function() {
/* code goes here */
}());
On occasions I see the following being used:
(function() {
/* code goes here */
})();
Which is the correct one?
According to Douglas Crockford (the creator of jslint) the first one is less error prone when another developer reads your code. But not everyone has to respect this, both are fine though it is good to know what exists, and why.
When a function is to be invoked immediately, the entire invocation
expression should be wrapped in parens so that it is clear that the
value being produced is the result of the function and not the
function itself.
var collection = (function () {
var keys = [], values = [];
return {
get: function (key) {
var at = keys.indexOf(key);
if (at >= 0) {
return values[at];
}
},
set: function (key, value) {
var at = keys.indexOf(key);
if (at < 0) {
at = keys.length;
}
keys[at] = key;
values[at] = value;
},
remove: function (key) {
var at = keys.indexOf(key);
if (at >= 0) {
keys.splice(at, 1);
values.splice(at, 1);
}
}
}; }());
Its purely an aesthetic preference. Use whatcha like. Douglas Crockford tried real hard to popularize the first, but I more often see the second.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Javascript infamous Loop problem?
I have the following code:
function test() {
var columns = options.columns;
for (var i =0; i < columns.length; i++) {
if (columns[i].type === "number") {
var field = columns[i].field;
columns[i].footerTemplate = function(data) { return buildFooter(data, field); };
}
}
}
function buildFooter(data, field) {
alert(field);
}
A library function calls the footerTemplate function (which in turn call buildFooter). The alert in buildFooter states that field is always the same value (the last value iterated in for loop of test.) How can buildFooter be called with the appropriate field value (i.e.
columns[0].footerTemplate = function(data) { return buildFooter(data, columns[0].field);}
and
columns[1].footerTemplate = function(data) { return buildFooter(data, columns[1].field);}
and so on...
Javascript does not scope variables inside logical blocks (loops, ifs). That field variable is shared across all the footerTemplate properties.
You can solve this by creating an inline function to create some scope:
for (var i =0; i < columns.length; i++) {
if (columns[i].type === "number") {
(function () {
var field = columns[i].field;
columns[i].footerTemplate = function(data) {
return buildFooter(data, field);
};
})();
}
}
}
Try:
columns[i].footerTemplate = (function(field) {
return function (data) {
buildFooter(data, field);
};
})(field);
It immediately executes a function that returns a new function that has the correctly bound field variable. It's definitely a scope issue that is a problem with loops, so using an immediately invoked function to create a new scope and making the correct variables available. This is definitely a duplicate of Javascript infamous Loop issue? though
Javascript is function-scoped language, so your decleration of
field
variable inside of for loop is the same as you declared it outside of foor loop, you're just overwriting same variable over and over again, there's no memory allocated for field variable in every loop, it's one same memory space that's being written to.