2

javascript engine know the type of variables through their values:

<script>
 var x=5.01; //x if float
 var x="abc"; //x is string
 var x=true; //x is Boolean
</script>

but here:

<script>
var loadFile = function(event) {
    var image = document.getElementById('output');
    image.src = URL.createObjectURL(event.target.files[0]);
}
</script>

My question is: how come event ( passed as a function parameter ) is known as event object without explicit or implicit expression to define it??? what makes it an event object and not other type like float integer..??

Notice: here where fileLoad is called:

<input type=file id=profilepic accept="image/*" name=profilepic onchange="loadFile(event)">

4 Answers 4

1

what makes it an event object and not other type like float integer..??

Nothing. There's no reason you couldn't write:

loadFile(42);

in which case event within that call would be the number 42, not an event.

The only thing that makes event an event object during the call to loadFile is the code that's calling it. For instance, if it's hooked up as an event handler to a DOM element, the DOM event handling code in the browser calls that handler with an event object as the first argument.

If you want to enforce type safety in your code, you need something other than JavaScript such as TypeScript (which compiles to JavaScript), Flow, JSDoc+IDE support, etc. These all layer a static type system on top of JavaScript which can help you catch errors where a function is expecting (say) an event object but you're calling it with a number instead.

Sign up to request clarification or add additional context in comments.

2 Comments

if i understanded well ..calling loadFile in here makes event known as event object : <input type=file id=profilepic accept="image/*" name=profilepic onchange="loadFile(event)">
@FReader04 - Yes, although I urge you not to use onxyz-attribute-style event handlers; I'd suggest using addEventListener instead or using an MVC-ish library like React or Vue. But yes, in that example, the call to loadFile executes in a scope generated by the browser that has an in-scope identifier called event that refers to an event object for that change event, so loadFile(event) calls loadFIle and passes that object into the function.
1

The value of a function argument is determined when the function is called.

When something calls the function it may or may not pass an event object.

If it does. Great. If it doesn't then you'll get an exception because it will fail to read event.target from the argument.

The argument name event is just a variable name. It doesn't enforce type safety.

var loadFile = function(event) {
  var image = document.getElementById('output');
  image.src = URL.createObjectURL(event.target.files[0]);
}

loadFile(2020);

2 Comments

i got it !! i have added other piece of code..please review my question.
@FReader04 — So you're reading the global event variable and passing that.
0

I think it's because JavaScript is weakly-typed. It just checks if the parameter has the necessary properties to run the function and it does so to the best of its capacities, and more often than not you end up with bugs rather than errors. So use TypeScript instead lol.

1 Comment

TypeSript...but why isn't famous as much as javascript..i din't hear about it until you mention it.
0

Javascript doesn't enforce any types. It's up to you, the programmer, to know what type your function is going to get. You can always check what type your parameters are using typeof to make sure it matches what you expect.

1 Comment

got it!! i have added other piece of code..please review my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.