javascript initial capital function error in browser - javascript

i have following function to make the value of control Initial capital.
ctrl.value = ctrl.value.toLowerCase().replace( /\b[a-z]/g , function {
return arguments[0].toUpperCase();
});
When i run this in browser i get the following error in console
SyntaxError: missing ( before formal parameters
whats wrong with the syntax.

Your function definition is missing the () parenthesis.
// -------------------------------------------------------------- vv
ctrl.value = ctrl.value.toLowerCase().replace(/\b[a-z]/g, function() {
return arguments[0].toUpperCase();
});

Solved by changing function { to function () {

Related

Function not defined in Javascript Console

I don't know why my function in the console shows not defined. I have tried to make it right and I just can't seem to get it. It works fine until I try to use the setInterval function. Also the build keeps saying I am missing a semicolon, but I just don't see it.
$(document).ready(function () {
var machineDataViewModel = {
machineDataItems: ko.observableArray([]),
loadMachineDataItems: function DataLoad() {
machineDataViewModel.machineDataItems.length = 0;
$.getJSON("http://localhost/JsonRestful/Service1.svc/GetMachineData", function (data) {
$.each(data.GetMachineDataResult, function (index, item) {
machineDataViewModel.machineDataItems.push(new machineDataModel(item));
});
});
}
};
ko.applyBindings(machineDataViewModel);
machineDataViewModel.loadMachineDataItems();
setInterval(DataLoad, 9000);
});
function machineDataModel(item) {
this.mach_no = ko.observable(item.mach_no),
this.VAR1 = ko.observable(item.VAR1),
this.VAR2 = ko.observable(item.VAR2),
this.VAR3 = ko.observable(item.VAR3),
this.VAR4 = ko.observable(item.VAR4)
};
You can't define the DataLoad() function the way you are and expect it to be available in your setInterval(). It simply doesn't work that way. The symbol DataLoad is only available inside the scope of that function. Instead, you can call it as:
setInterval(machineDataViewModel.loadMachineDataItems, 9000);
Here's a simple demonstration that shows you can't name your function like you are and expect to use that name outside that scope: http://jsfiddle.net/jfriend00/6t0pp60s/ (look in the debug console to see the error).
FYI, if you need your function to have the right value of this (which I don't think you actually do), then you would call it like this (with a semi-colon at the end of every line):
setInterval(machineDataViewModel.loadMachineDataItems.bind(machineDataViewModel), 9000);
As for the semicolon issue, jsHint points to the this.VAR4 assignment line. I'd suggest changing machineDataModel() to this (which gives you a clean bill of health in jsHint):
function machineDataModel(item) {
this.mach_no = ko.observable(item.mach_no);
this.VAR1 = ko.observable(item.VAR1);
this.VAR2 = ko.observable(item.VAR2);
this.VAR3 = ko.observable(item.VAR3);
this.VAR4 = ko.observable(item.VAR4);
}

Is it possible to determine whether a certain function may be called when a passed-in callback function is executed in JavaScript?

The question title basically says it all. I made this Fiddle to make it easy to test. Here's the code:
var test = function(callback) {
console.log("callback() might call alert()");
callback();
}
test(function() {
alert("one");
});
Converting a function to a string returns the source code, you can search that with a regular expression.
var test = function(callback) {
if (callback.toString().test(/\balert\s*\(/) {
console.log("callback might call alert()");
callback();
};
This isn't perfect, though. It won't work with:
var foo = alert;
test(function() {
foo("Fooled you!");
});
It could also get a false positive if alert( appears in a string.
You can redefine function alert
function alert(str) {
console.log('Somebody call alert:'+str);
}
But IMHO it can't be definitely checked before call.

Meteor.setTimeout() issue in Meteor Timers?

I did a sample example on Meteor.setTimeout() using Meteor. In this example i get an error. I didn't have any idea about this.So please see the below code,error and suggest me how to do?
Error :
Exception in setTimeout callback: TypeError: undefined is not a function
at _.extend.withValue (http://localhost:3000/packages/meteor.js?8ec262df25783897eaad01255bc8bd1ca4e78b24:773:17)
at http://localhost:3000/packages/meteor.js?8ec262df25783897eaad01255bc8bd1ca4e78b24:358:45
at http://localhost:3000/packages/meteor.js?8ec262df25783897eaad01255bc8bd1ca4e78b24:801:22
JS Code :
if (Meteor.isClient)
{
Meteor.setTimeout(Test("10"), 1000);
Meteor.setInterval(Test1, 1000);
Template.hello.greeting = function ()
{
return "Welcome to timerapp.";
};
Template.hello.events
({
'click input' : function ()
{
// template data, if any, is available in 'this'
if (typeof console !== 'undefined')
console.log("You pressed the button");
//Test();
}
});
}
function Test(x)
{
console.log("*** Test() ***"+x);
}
function Test1()
{
console.log("*** Test1() ***");
}
if (Meteor.isServer)
{
Meteor.startup(function ()
{
// code to run on server at startup
});
}
The problem is that setTimeout expects a function as a first parameter but you are passing the result of evaluating Test("10") which is "undefined".
You can solve the issue by wrapping your call to Test1 in an anonymous function:
Meteor.setTimeout(function(){Test("10");}, 1000);

Why Does Assigning console.log to a variable cause an illegal invocation? [duplicate]

I'm creating a lambda function that executes a second function with a concrete params. This code works in Firefox but not in Chrome, its inspector shows a weird error, Uncaught TypeError: Illegal invocation. What's wrong with my code?
var make = function(callback,params){
callback(params);
}
make(console.log,'it will be accepted!');
The console's log function expects this to refer to the console (internally). Consider this code which replicates your problem:
var x = {};
x.func = function(){
if(this !== x){
throw new TypeError('Illegal invocation');
}
console.log('Hi!');
};
// Works!
x.func();
var y = x.func;
// Throws error
y();
Here is a (silly) example that will work, since it binds this to console in your make function:
var make = function(callback,params){
callback.call(console, params);
}
make(console.log,'it will be accepted!');
This will also work
var make = function(callback,params){
callback(params);
}
make(console.log.bind(console),'it will be accepted!');
You can wrap the function which need 'this' to a new lambda function, and then use it for your callback function.
function make(callback, params) {
callback(params);
}
make(function(str){ console.log(str); }, 'it will be accepted!');

Why doesn't Javascript let me close up my function?

I dunno guys, this is a really weird one, but I might just be making a simple mistake and not even realizing it.
I'm sort of a newbie to Javascript, so I'm attempting to write a script that gets content from a PHP script (which returns only a number) and write that data to a div... but Javascript had other ideas. I'm testing on Chrome on Mac OS X, although it doesn't work on Safari either.
The following block is giving me problems:
function getContent() {
window.setInterval(function () {
$.get("get.php", function (data) {
$('#img').slideUp();
$('#div').html(data);
$('#div').slideDown();
}
}
}
Which is failing with:
Uncaught SyntaxError: Unexpected token }
on line 51, or line 8, for the purposes of this example.
Does anyone know why it would fail like this? Don't I need to close the brackets I open?
Your curly braces are OK, but you're missing a few parentheses:
function getContent() {
window.setInterval(function () {
$.get("get.php", function (data) {
$('#img').slideUp();
$('#div').html(data);
$('#div').slideDown();
}); //get - end statement
}, 4000); // setInterval - need another parameter, end statement
}
You're not closing the parentheses for your function calls. As Kobi said, you also need a third parameter for setInterval.
function getContent() {
window.setInterval(function () {
$.get("get.php", function (data) {
$('#img').slideUp();
$('#div').html(data);
$('#div').slideDown();
});
}, 1000);
}
The window.setInterval function has a following syntax:
window.setInterval(functionRef, timeout);
In your case the setInterval and $.get() function call are missing the closing parentheses ). It would be clear for you to write this in the following way:
function getContent() {
// success handler
var success = function() {
// declare the function first as "changeUI"
var changeUI = function() {
$('#img').slideUp();
$('#div').html(data);
$('#div').slideDown();
};
// call the setInterval passing the function
window.setInterval(changeUI, 2000);
};
// get the resource and call the "success" function on successful response
$.get("get.php", success);
}
your window.setInterval is missing a ) after the } on the second to last line

Categories