0

I have a js library which has a set of functions. The library is in a file libary.js. I should use the library as shown below :

function (AUTH_OBJ){
var botApi= new library(AUTH_OBJ);
var value = botApi.getAttribute("num3");
var value2 = botApi.getAttribute 
botApi.setInCache("multiply",789);
botApi.setAttribute("num",700);
botApi.setInCache("/numbers/data/num1",300);

botApi.setInCache("/multiply/new",5);
var fromCache = botApi.getFromCache("/numbers/data/num1")
return botApi.done();
}

The library does not have "getAttribute" function as used in the line

var value2 = botApi.getAttribute 

But the code still executes. I want to throw an error when an un-declared variable is referenced in the library. I am unable to figure out a way. Please help me out.

codejammer
  • 469
  • 1
  • 7
  • 18
  • use strict mode and put try catch and handle the error – Kamran Shahid Jan 31 '14 at 09:51
  • @KamranShahid: How does strict mode help with accessing undefined properties? – Felix Kling Jan 31 '14 at 09:51
  • 1
    If `botApi.getAttribute` does not exist, your code will already throw an error in the previous line: `botApi.getAttribute("num3");`. You don't have to do anything! I doubt that your code executes, unless you never call the function of course (in which case you also cannot test whether the property exists or not). *Why* exactly are you trying to do this anyway? Please provide more information. – Felix Kling Jan 31 '14 at 09:53
  • Check http://stackoverflow.com/questions/1335851/what-does-use-strict-do-in-javascript-and-what-is-the-reasoning-behind-it – Kamran Shahid Jan 31 '14 at 09:54
  • I agree with @FelixKling. If `getAttribute` is not defined in `botApi` then you should already get a **TypeError** (`Object # has no method 'getAttribute'`) – fardjad Jan 31 '14 at 09:57
  • 1
    Also please note that your title is *very* misleading: Trying to access an undeclared variable will *always* throw a (runtime) error. It looks like you want to throw an error if an *undefined property* is accessed, which is something completely different. – Felix Kling Jan 31 '14 at 09:58
  • @KamranShahid: Oh, was this a comment for me? I know what strict mode is, I was just wondering what difference it would make regarding accessing undefined properties. AFAIK it doesn't make a difference, but maybe you know something I don't? – Felix Kling Jan 31 '14 at 10:00
  • no it was for codejammer.I have very limited knowledge in comparison to geek like you – Kamran Shahid Jan 31 '14 at 10:02
  • `getAttribute(attributename)` is a function in the library and not a variable. I am trying to handle everything in the library. I want to throw an error if someone wants to access a variable which is not defined. In this case it is `getAttribute` as a varible – codejammer Jan 31 '14 at 12:46

2 Answers2

3

botApi.getAttribute should return undefined. You can check that and throw an error:

if (botApi.getAttribute === undefined) {
  throw new TypeError("getAttribute is not defined");
}
fardjad
  • 20,031
  • 6
  • 53
  • 68
0

if you want to check inexistent properties, you can use hasOwnProperty

var foo = {};
if(! foo.hasOwnProperty('attribute'))
{
   console.log('Missing attribute!');
}
markcial
  • 9,041
  • 4
  • 31
  • 41