What does it mean when an object has no method in JavaScript? - javascript

I get the following error:
Uncaught TypeError: Object #<an Object> has no method 'indexOf'
for this line:
else if(file.indexOf(".mp3") == file.length - 4 && return_path === false)
What does this mean? Which is the object? What is a method? And what would be am simple example of an applied method? I'm really new to JavaScript so please keep it simple.

What does this mean?
It simply means that whatever type file is at the moment, it doesn't have a .indexOf() method, in this case that means it's not the string you think it is/should be.
Wich is the object? file
What is a method? 0 like any other language, objects have methods, .indexOf() is what it's trying to run here.
And what would be am simple example of an apllied method? I'm really new to javascript so please keep it simple.
I'm not sure what you mean by "applied method", but everything here just resolved around calling a method on an object, for example file.split(".") to turn it into an array, etc...any method String has.

Related

JavaScript Object Syntax & Notation

I've been using JavaScript for a short period of time, and have found myself interested as to whether or not there is any correlation between the . syntax used for referencing children of an object, and functions such as console.log.
And was interested whether functions such as this, were in a way objects behind the scenes, with log being a method inside of a console object, and if I am just referencing the child of an object.
Or another example, the .length method, is this just a hidden property of the object or array you are referencing.
Apologies if this is poorly worded, I was struggling to write down my train of thought, and I would be incredible appreciative if anyone could let me know if this is how methods such as these are structured, or if I am thinking of them in completely the wrong way.
Notice how you can access the length property on a string. While string is a primitive, the length property actually exits on a wrapper object that is created temporarily when we try to read .length.
Arrays and other collections have this property, because they are objects themselves.
Hope this helps to clarify a bit!
console.log is not a function, per se. The function is the log property on the console object. console is an object with many methods:
To access it's method, just like to access any property of a JS object, you can use dot notation or square bracket notation
var log = console.log;
log('hello');
var logSquare = console['log'];
logSquare('hello');
// or just do
console['log']('hello');
Except for primitives, everything in JS is an object.

Is it safe in Javascript to assume the existence of "constructor" property on variables with values other than "null" or "undefined"?

I was wondering if it is safe to assume that in JavaScript a variable will always have a "constructor" property if its value is not "null" or "undefined".
I ran into a situation where I had to check if a variable is defined and that if it is an array and if so check if its length is > 0:
if(variable && variable.constructor === Array && variable.length > 0) {
...loop through the array
}
Am I right to assume that variable will always have a constructor if it is not "undefined" or "null"?
Your if statement won't throw because you've made that assumption, because anything that passes your first check will support your second, even if there is no constructor property (you'll just get undefined), and in fact even if it's not an object (it'll get temporarily promoted). So in that sense yes, you can happily do what you're doing — unless you could be dealing with an array from another window, in which case the === Array will fail (as dandavis pointed out) because different windows have different instances of the Array constructor.
Note that this is not the same as every object having a constructor property. It's just that if the object doesn't, you'll get undefined, rather than an error. (You can get an object with no constructor property by creating an object with no prototype: var obj = Object.create(null); or by using an object whose prototype is null as the object's prototype.)
However, somewhat tangentially, I wouldn't use that mechanism to check whether something is an array. In any modern environment, I'd use Array.isArray; and I'd shim/polyfill it on older environments. This also has the happy effect of working with arrays from other windows.
undefined or null do not have constructors and will error if you try to get it. Just try it out on Ctrl+Shift+I, and play around with it. Though, I doubt your if statement would cause much trouble, and it'd most likely error on pratical use.
You do NOT need to check length or anything, only see if it exists (or if you want specifically array, you could check that aswell).

Javascript object dump returns multiple objects

I am confused with what I am seeing on a web site. From the console I type dataLayer.dump(). This outputs in the console 4 objects. Pretty sure that's weird and my research suggests a javascript method should only be able to return one object. If you need to return more than one you stick it into an array.
However, when I try dataLayer.dump()[0] I get undefined.
My question is: does anyone know if its possible to return multiple objects from one function call (not meaning an array). Or is it likely I am using the console wrong and confused myself with it? Any ideas on how to access the objects?
Why dont you type
dataLayer.dump
which will show you the code of function?
Looks like it will be something like this:
function dump(){
console.log({a:1,b:2},{a:1,b:2},{a:1,b:2},{a:1,b:2});
}

define method on built-in type in javascript

Problem for example: In general null object means additional class which handle cases when object not found etc.
In Ruby you can also define method for Nil class. And I want realise something similiar in js.
That's what gives me hope typeof(null) //=> object, but in same time null instanceof Object //=> false. So defining method for Object gives me nothing.
So the final question: is there any hacks to define method on built-in types?
Please don't.
That's really bad form and will lead to nasty, hard to find, bugs.
Or if you do - do it with greatest of cares.
Read here for more: http://perfectionkills.com/extending-built-in-native-objects-evil-or-not/
You can do that in javascript too.
I am not sure whether I understand your question.But here is my take on it.
alert(typeof(null));//gives you object.
MyObject=Object.create(null);
This means
MyObject doesn't inherit from anywhere
MyObject has no properties at all.

How I could be able to compare undefined in JS?

Hi I'm very new to JS.
Lets say I have an object named test which doesn't have a property missing. But I try to access the same:
test.missing //getting undefined as output
Now I'm trying to access the missing properties ( like this test.missing.404 ) which will give me a TypeError. But my book says we can get rid of this TypeError by like this:
test.missing && test.missing.404
Now my big question is how one can do a && against the undefined type and TypeError type. Couldn't able to guess what Js is doing here.
Thanks in advance.
undefined in js is treated as false and if something has value it is treated as true, so:
//if test.missing is NOT undefined and test.missing.404 is also NOT undefined
if (test.missing && test.missing.404)
First of you should know that JS like many other programming languages uses short circuit technique during comparison. So in this case if the first condition is false then there is no need to check the next condition(s). This is due to the fact that 0 && X will always result 0 .
With that said, test.missing && test.missing.404 can be perceive as "if the test object has missing property and the (test object's) missing property has 404 property" then proceed. Which the JS intepreter will completely ignore the check for 404 property on missing property if the test object has no missing property in the first place.
So the point is the above code is not && comparison of undefined and TypeError but rather check for missing property first and 404 property if first property exists. I hope this makes sense ad explains it well enough.

Categories