I have 3 questions. Thank!
First question:
When do JavaScript codes throw a TypeError exception?
Other questions:
I have the codes below:
<!DOCTYPE html>
<meta charset="utf-8">
<title>An HTML5 document</title>
<script>
var str = 'abc';
alert(Object.getPrototypeOf(str));
if (Object.prototype.isPrototypeOf(str)) {
alert('true');
} else {
alert('false');
}
</script>
The method getPrototypeOf()and isPrototypeOf()requires a parameter whose type must be an object. And the type stris a string.
Why does the method getPrototypeOfthrow a TypeError exception and the method isPrototypeOfdoes not raise errors?
If the type stris an object ( var str = new String('abc')), the result Object.prototype.isPrototypeOf(str)is true. But the result is above codes false. Why is it strnot converted from a string to an object automatically when used as a parameter to a method isPrototypeOf?
Thank!
source
share