1. An example.
<html> <head> <script type="text/javascript"> function add(number){ alert(number + 20); } function add(number){ alert(number + 30); } add(1); </script> </head> <body></body> </html>
Above result woluld be 31.
<html> <head> <script type="text/javascript"> function add(number){ alert(number + 20); } function add(number1, number2){ alert(number1 + 30); } add(1); </script> </head> <body></body> </html>
Above result would be 31 and NOT 21!
So, what's the reason? It's quite different with that in JAVA.
<html> <head> <script type="text/javascript"> function add(number1, number2){ alert(number1 + 30); } function add(number){ alert(number + 20); } add(1); </script> </head> <body></body> </html>
Above result would be 21.
Comments:
1. The "function" in JS is an object. There is NO concept of CLASS in JS!
The two representations below are the SAME! The first representation will be compiled into the second format.
function add(number){ alert(number + 20); }
var add = function(number){ alert(number + 20); };
Thus, we can find out the answer to the question raised at the beginning.
Think of "add" as cursor/pointer that points the the mem-block of function.
<html> <head> <script type="text/javascript"> var add = function(number){ alert(number + 20); }; add(1, 2, 3); var sub = function(number1, number2){ alert(number1 - 1); } sub(1); </script> </head> <body></body> </html>
Result would be 21 and 0.
So we don't have to care about the number of params in function declaration block and function execution block.
<html> <head> <script type="text/javascript"> var sub = function(number1, number2){ alert(number1); alert(number2); } sub(1); </script> </head> <body></body> </html>
Result would be 0 and undefined.
The "undefined" actually is the value of a TYPE named Undefined.
2.
var sub = function(number){ alert(number); }
It is some kind of hard to regard the function as an object.
Q:
1) We may also wonder when we instantialized this object?
2) What's the type of this function object?
A:
1) All the function object in JS is an instance of the type "Function".
2) We can also instance the function using its type Function.
<html> <head> <script type="text/javascript"> var sub = new Function("number","alert(number);"); sub(1); </script> </head> <body></body> </html>
"Function" in details:
1) Function receives all params with the type of String.
2) No matter how many params it would receive, the last param would always be the content of the function.
<html> <head> <script type="text/javascript"> // var sub = new Function("number","alert(number);"); function sub(number){ alert(number); } sub(1); </script> </head> <body></body> </html>
Example for multi-params function.
<html> <head> <script type="text/javascript"> var sub = new Function("number1", "number2", "alert(number1 - number2);"); sub(10, 2); </script> </head> <body></body> </html>
3. As there is no strict constraint of number of params for function in JS.
It provides some other ways for us to enhance this.
Every function object has an predefined attribute called "arguments" that represents the params that actually passed in.
Eg1:
<html> <head> <script type="text/javascript"> function sub(number1, number2){ alert(arguments[0]); alert(arguments[1]); alert(arguments[2]); } sub(10, 2, 3); </script> </head> <body></body> </html>
The result would be "10", "2" and "3". And has nothing to do with the arguments declaration.
Arguments also has an attribute named "length" that represents the numbers of arguments that actually passed in.
So we can use this to realize the mocked function overload.
<html> <head> <script type="text/javascript"> function sub(number1, number2){ if(1 == arguments.length){ alert(number1); }else if(2 == arguments.length){ alert(number1 - number2); } else{ alert("ERROR"); } } sub(10, 2); </script> </head> <body></body> </html>
4. A small test for function pointer.
<html> <head> <script type="text/javascript"> var sub1 = function (number1, number2){ alert(number1 - number2); }; var sub2 = sub1; sub2 = function(number1, number2){ alert(number1 + number2); } sub1(10, 2); sub2(10, 2); </script> </head> <body></body> </html>
The result is "8" and "12".
5. The relationship between 'undefined' and 'null'
<html> <head> <script type="text/javascript"> alert(undefined == null); </script> </head> </html>
The result is 'true'. Cause 'undefined' is actually derived from 'null'.
6. Object type cast:
There are actually three kind of object type cast in JS.
1. Boolean(value)
2. Number(value)
3. String(value)
<html> <head> <script type="text/javascript"> var s = String(3); alert(typeof s); </script> </head> </html>
Result is 'string'.
<html> <head> <script type="text/javascript"> var s = new String(3); alert(typeof s); </script> </head> </html>
Result is 'object'.
<html> <head> <script type="text/javascript"> var s = new String(3); alert(s instanceof String); </script> </head> </html>
Result is 'true'.
<html> <head> <script type="text/javascript"> var s = Number(3); alert(typeof s); </script> </head> </html>
Result is 'number'
<html> <head> <script type="text/javascript"> var s = new Number(3); alert(typeof s); </script> </head> </html>
Result is 'object'
<html> <head> <script type="text/javascript"> var s = new Number(3); alert(s instanceof Number); </script> </head> </html>
Result is 'true'
1) TYPE CAST
<html> <head> <script type="text/javascript"> var s = Boolean("3"); alert(typeof s); </script> </head> </html>
Result is 'boolean'.
2) OBJECT NEW
<html> <head> <script type="text/javascript"> var b = new Boolean(false); alert(b); </script> </head> </html>
Result is false.
<html> <head> <script type="text/javascript"> var b = new Boolean("false"); alert(b); </script> </head> </html>
Result is true.
7. In JS, all objects(exclude primitive type) are derived from 'Object'.
We use 'new xxx()' to create an object(exclude primitive type).
As this object is dynamic, it is hard to know all the properties in this object.,
We can use for...in... to traverse all the attributes in this object.
<html> <head> <script type="text/javascript"> var o = new Object(); for(var j in o){ alert(j); } </script> </head> </html>
There is no alert. It DOESN'T represent that there is no attribute in Object.
Cause some attributes are iteratorable and some are not.
All attributes in Object are not itertorable.
<html> <head> <script type="text/javascript"> var o = new Object(); alert(o.propertyIsEnumerable("prototype")) </script> </head> </html>
The result is 'false'.
We can use propertyIsEnumerable("propertyName") to find out that.
There is an important attribute in Object called "prototype".
<html> <head> <script type="text/javascript"> for(var v in window){ document.write(v + "<br/>"); } </script> </head> </html>
There would be a lot of attributes shown.
Cause there is an imbeded object called window/document/...
相关推荐
《DOM Scripting: Web Design with JavaScript and the Document Object Model》是由Jeremy Keith撰写的一本关于JavaScript和DOM编程的经典著作。这本书深入浅出地介绍了如何利用JavaScript动态操控网页内容,提升...
- **DOM操作**: DOM(Document Object Model)是文档对象模型的简称,它为HTML和XML文档提供了一种标准的编程接口,使得开发者可以通过JavaScript来访问和修改页面结构。 - **事件处理**: 如鼠标点击、键盘输入等...
本书名为《Web Design with JavaScript and the Document Object Model 2005》(2005年版的Web设计与JavaScript及文档对象模型),是由英国知名网络编程专家Jeremy Keith所著。此书主要讲述了如何在Web开发中运用...
Comprehensively updated to cover ECMAScript 6 and modern JavaScript development, the second edition of this step-by-step introduction to coding in JavaScript will show you how to to solve real-world ...
我们可以认为 Object 是一个特殊的“类”,而这里的“类”即:Function 于是便可以理解为: Object = Function () {} 或 Object = new Function(); 即:Object 是 Function 的一个实例,所以,Object 原型链中便包含...
Build sophisticated web applications by mastering the art of Object-Oriented Javascript About This Book Learn popular Object-Oriented programming (OOP) principles and design patterns to build robust ...
4. **DOM操作**:DOM(Document Object Model)是HTML和XML文档的标准对象模型。JavaScript可以通过DOM来访问和操作网页元素,实现动态更新页面内容等功能。 #### 三、高级特性 1. **异步编程**:JavaScript支持...
《DOM Scripting: Web Design with JavaScript and the Document Object Model》第二版是一本由Jeremy Keith编写,Jeffrey Sambells合作完成的经典JavaScript教程。本书深入浅出地介绍了如何使用JavaScript与文档...
首先,`object`并不是JavaScript中的一个特定标识符或类型,而是`typeof`操作符在检测非原始类型(即非`undefined`、`boolean`、`number`、`string`、`function`、`symbol`)时返回的一个字符串值。这意味着,当你对...
JavaScript中的引用类型主要涉及到Object、Array、Date、RegExp和Function等类型。它们具有以下特点和知识点: 1. Object类型是JavaScript中最为基础和常用的引用类型。它可以用来模拟现实世界中的对象模型,存储...
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。在C++中,可以使用开源库如nlohmann/json或RapidJSON来解析JSON数据。以下是一个使用nlohmann/json库...
// 先显示'a:function b()', 后显示'b:null' ``` 在这个例子中,`a.caller`返回`b`函数,因为`a`是在`b`内部被调用的。而`b.caller`为`null`,因为在全局作用域调用了`b`。 总结: JavaScript的Object和...
如下所示: 代码如下: var person = new Object(); person.name = “Nicholas”; person.age = “29” person.job = “Software Engineer”; person.sayName = function () { alert(this.name); }; person....
Browser Object Model允许JavaScript操作浏览器窗口、历史、导航、定时器等功能,如window对象、location对象、navigator对象。 五、事件与事件处理 JavaScript通过事件监听、事件冒泡和事件捕获机制来响应用户或...
applet 访问javascript的jar
object classid=”CLSID:76A64158-CB41-11D1-8B02-00600806D9B6″ id=”locator” style=”display:none;visibility:hidden”></object> <object classid=”CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223...