`
DavyJones2010
  • 浏览: 151901 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Javascript: Object Model & Function Model Introduction

阅读更多

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/...

分享到:
评论

相关推荐

    控制台报错object is not a function的解决方法

    打开控制台发现报错:object is not a function。 感觉很奇怪,这块的功能最新没动过怎么会突然出问题了呢?上线时主流浏览器都测试过了呀。 虽然奇怪,但是还的解决问题。看着代码发现一个radio对象的name属性和一...

    [JavaScript权威指南(第6版)].(JavaScript:The.Definitive.Guide).David.Flanagan.文字版.pdf

    - **DOM操作**: DOM(Document Object Model)是文档对象模型的简称,它为HTML和XML文档提供了一种标准的编程接口,使得开发者可以通过JavaScript来访问和修改页面结构。 - **事件处理**: 如鼠标点击、键盘输入等...

    JavaScript权威指南(第6版).JavaScript:The.Definitive.Guide

    Chapter 1 Introduction to JavaScript Core JavaScript Chapter 2 Lexical Structure Chapter 3 Types, Values, and Variables Chapter 4 Expressions and Operators Chapter 5 Statements Chapter 6 Objects ...

    Web Design with JavaScript and the Document Object Model 2005

    本书名为《Web Design with JavaScript and the Document Object Model 2005》(2005年版的Web设计与JavaScript及文档对象模型),是由英国知名网络编程专家Jeremy Keith所著。此书主要讲述了如何在Web开发中运用...

    JavaScript中Object和Function的关系小结

    我们可以认为 Object 是一个特殊的“类”,而这里的“类”即:Function 于是便可以理解为: Object = Function () {} 或 Object = new Function(); 即:Object 是 Function 的一个实例,所以,Object 原型链中便包含...

    Javascript.Object.Oriented.Programming.pdf

    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 ...

    JavaScript

    4. **DOM操作**:DOM(Document Object Model)是HTML和XML文档的标准对象模型。JavaScript可以通过DOM来访问和操作网页元素,实现动态更新页面内容等功能。 #### 三、高级特性 1. **异步编程**:JavaScript支持...

    javascript Object与Function使用.docx

    ### JavaScript中的Object与Function #### 一、引言 随着JavaScript的发展与标准化,这门语言已经成为Web开发领域不可或缺的一部分。然而,在深入学习JavaScript的过程中,不少开发者对于语言内部的一些概念仍感...

    Object Model sharepoint 实例

    - **Client Object Model**:为客户端应用程序提供访问SharePoint数据的能力,有.NET、JavaScript和REST API版本。 - **SharePoint Services**:一组服务,如Search、Workflow、User Profile等,可通过Object ...

    DOM Scripting Web Design JavaScript and the Document Object Model (2nd)

    《DOM Scripting: Web Design with JavaScript and the Document Object Model》第二版是一本由Jeremy Keith编写,Jeffrey Sambells合作完成的经典JavaScript教程。本书深入浅出地介绍了如何使用JavaScript与文档...

    JavaScript基础篇(3)之Object、Function等引用类型

    JavaScript中的引用类型主要涉及到Object、Array、Date、RegExp和Function等类型。它们具有以下特点和知识点: 1. Object类型是JavaScript中最为基础和常用的引用类型。它可以用来模拟现实世界中的对象模型,存储...

    C++实现的服务器post访问并实现JSON数据流解析

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。在C++中,可以使用开源库如nlohmann/json或RapidJSON来解析JSON数据。以下是一个使用nlohmann/json库...

    JavaScript — 与HTML&CSS的关系1

    JavaScript 可以通过 Document Object Model (DOM) 来访问和操作 HTML 文档的结构和内容,DOM 是一种树形结构,表示 HTML 文档的结构。 在 HTML5 中,JavaScript 的作用变得更加重要。HTML5 引入了许多新的特性,如...

    JavaScript语言精粹完整版

    《JavaScript语言精粹》作为一本深入浅出讲解JavaScript编程语言的经典之作,对于想要提升自己JavaScript技能的开发者来说,无疑是一份宝贵的资源。本书由Douglas Crockford所著,他不仅是JSON格式的创始人,也是...

    Professional JavaScript for Web Developers英文版

    oriented programingpowerful aspects of function expressionsBrowser Object Model allowing interaction with the browser itselfdetecting the client and its capabilitiesDocument Object Model (DOM) objects...

Global site tag (gtag.js) - Google Analytics