[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[学习笔记]pro javascript techniques学习笔记1

上一篇:[备忘]设置Ext Js的下拉框的长度
下一篇:[转]在Seam项目中快速实现Web Service

添加日期:2009/2/15 7:12:13 快速返回   返回列表 阅读3988次
学习笔记2-->>
http://www.mytju.com/classcode/news_readNews.asp?newsID=235

javascript中的引用

1.一个对象可以包含多个属性,每个属性都是对其它对象(如字符串,数字,数组等)的引用。
如果多个变量指向同一个对象,改变底层的这些对象,则会影响所有的变量。


<script>
// Set obj to an empty object
var obj = new Object();
// objRef now refers to the other object
var objRef = obj;
// Modify a property in the original object
obj.oneProperty = true;
// We now see that that change is represented in both variables
// (Since they both refer to the same object)
alert( obj.oneProperty === objRef.oneProperty );
</script>


结果为true.

2.在数组Object的内部,值也是作为属性存储的,所以,效果同上。


<script>
// Create an array of items
var items = new Array( "one", "two", "three" );
// Create a reference to the array of items
var itemsRef = items;
// Add an item to the original array
items.push( "four" );
// The length of each array should be the same,
// since they both point to the same array object
alert( items.length == itemsRef.length );
</script>


结果为true.

3.引用是直接指向最终被引用的对象的,不是引用本身。


<script>
// Set items to an array (object) of strings
var items = new Array( "one", "two", "three" );

// Set itemsRef to a reference to items
var itemsRef = items;
// Set items to equal a new object
items = new Array( "new", "array" );
// items and itemsRef now point to different objects.
// items points to new Array( "new", "array" )
// itemsRef points to new Array( "one", "two", "three" )
alert( items !== itemsRef );
</script>


结果为true,itemsRef仍然指向原来的数组。

4.字符串还是比较特殊……不能改变,变更了,返回的就是新的。


<script>
// Set item equal to a new string object
var item = "test";
// itemRef now refers to the same string object
var itemRef = item;
// Concatenate some new text onto the string object
// NOTE: This creates a new object, and does not modify
// the original object.
item += "ing";
// The values of item and itemRef are NOT equal, as a whole
// new string object has been created
alert( item != itemRef );
</script>


结果为true。

javascript中的方法重载和类型check
java中是支持方法重载的,也就是方法的参数类型和个数不同,就是不同的方法。
javascript中需要变通实现。
需要两个能力:检测有多少个参数被提供,检测被提供的参数的类型。

在javascript的每个function内,都存在一个变量arguments,它包含了所有传递进来的参数,它不是一个真正的属性,你不能修改它,或调用push()添加新项目。但是,你可以访问数组里的所有项目,它有length属性。


<script>
// A simple function for sending a message
function sendMessage( msg, obj ) {
    // If both a message and an object are provided
    if ( arguments.length == 2 )
        // Send the message to the object
        obj.handleMsg( msg );
    // Otherwise, assume that only a message was provided
    else
        // So just display the default error message
        alert( msg );
}
// Call the function with one argument – displaying the message using an alert
sendMessage( "Hello, World!" );
// Otherwise, we can pass in our own object that handles
// a different way of displaying  information
sendMessage( "How are you?", {
        handleMsg: function( msg ) {
            alert( "This is a custom message: " + msg );
        }
    });

// A function that takes any number of arguments and makes
// an array out of them
function makeArray() {
    // The temporary array
    var arr = [];
    // Go through each of the submitted arguments
    for ( var i = 0; i < arguments.length; i++ ) {
        arr.push( arguments[i] );
    }
    // Return the resulting array
    return arr;
}
</script>


显示为两次消息框,第一次为Hello, World!,第二次为This is a custom message:How are you?


另外一个方法,就是使用typeof,如果参数没有传,则它的type是undefined。


<script>
function displayError( msg ) {
    // Check and make sure that msg is not undefined
    if ( typeof msg == 'undefined' ) {
    // If it is, set a default message
        msg = "An error occurred.";
    }
    // Display the message
    alert( msg );
}
</script>



typeof返回变量内容的类型,但是object或数组,或自定义object,只会返回object。


<script>
// Check to see if our number is actually a string
if ( typeof num  == "string" )
// If it is, then parse a number out of it
num = parseInt( num );
// Check to see if our array is actually a string
if ( typeof arr == "string" )
// If that's the case, make an array, splitting on commas
arr = arr.split(",");
</script>



判断对象类型的另一个方法是使用constructor属性,它返回的是object.


<script>
// Check to see if our number is actually a string
if ( num.constructor == String )
// If it is, then parse a number out of it
num = parseInt( num );
// Check to see if our string is actually an array
if ( str.constructor == Array )
// If that's the case, make a string by joining the array using commas
str = str.join(',');
</script>



列表:
---------------------------------------------------------
Variable        typeof Variable        Variable.constructor
{an:“object”}         object             Object
[“an”,“array”]     object             Array
function(){}         function         Function
“a string”         string             String
55             number             Number
true             boolean         Boolean
new User()         object             User
---------------------------------------------------------
看来,用constructor来检测类型是最简单、最安全的。


变量的范围Scope
在javascript中,scope保持在function中,但不再块中,比如while,if,for语句。

<script>
// Set a global variabl
var foo = "test";

// Within an if block
if ( true ) {
// Set foo equal to 'new test'
// NOTE: This is still within the global scope!
var foo = "new test";
}
// As we can see here, as foo is now equal to 'new test'
alert( foo == "new test" );
// Create a function that will modify the variable foo
function test() {
var foo = "old test";
}
// However, when called, 'foo' remains within the scope
// of the function
test();
// Which is confirmed, as foo is still equal to 'new test'
alert( foo == "new test" );
</script>


两次都显示true。

在此例中,变量是全局范围的。事实上,在浏览器中使用javascript,全局变量就是window对象的一个属性。

<script>
// A globally-scoped variable, containing the string 'test'
var test = "test";
// You'll notice that our 'global' variable and the test
// property of the the window object are identical
alert( window.test == test );
</script>


显示true.

如果没有声明变量,则它是全局范围的,即使是在function内部使用。

<script>
// A function in which the value of foo is set
function test() {
    foo = "test";
}
// Call the function to set the value of foo
test();
// We see that foo is now globally scoped
alert( window.foo == "test" );
</script>


显示true.

 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved