JavaScript学习笔记

from http://www.howtocreate.co.uk/tutorials/javascript/

定义变量

JS里变量的类型并不是非常重要,变量的类型可以在运行期改变。基础类型有,character,string,integer,float(or double),boolean, function, object, array, undefined, null. Interger and float are grouped as ‘number’. Charater and string are grouped as string. array属于对象类型。

变量的定义:

var myFish = ‘A fish swam in the river’ 

对象定义:

var o1 = new Object(); 
var o2={myFirstProperty:1, myNextProperty:’hi’,etc};

数组的使用:

var nameOfArray = new Array();
var nameOfArray = new Array('content_of_first_cell','and_the_second',8,'blah','etc');
var nameOfArray = ['content_of_first_cell','and_the_second',8,'blah','etc'];
同时JavaScript可以定义多维数组:
var nameOfArray = new Array(new Array(1,2,3,4),'hello',['a','b','c']);

其它类型:Date,Image,Option,RegExp

If at any time you refer to a variable that does not exist, you will cause an error. However, JavaScript allows you to refer to one level of undefined child properties.

JavaScript中也有值类型和引用类型的概念,所有的基本类型为值类型,所有的object类型为引用类型。

var myNewVariable = myOldVariable;

If myOldVariable was already defined as a string, number, boolean, null or undefined, it would simply have copied myOldVariable and 'pasted' it into myNewVariable. If the new variable were changed (for example, using myNewVariable = 'some new value';)myOldVariable retains its old value.

If, on the other hand, myOldVariable was already defined as a function, array, object or option, myNewVariable would have been created as a pointer to myOldVariable. The children of myOldVariable would now also be the children of myNewVariable. If the new variable were changed (for example, using myNewVariable = 'some new value';), it would only alter the value of myNewVariable so that it no longer references the same contents as myOldVariable. However, changing the child properties of myNewVariable will change the properties of the object it references, and as a result, myOldVariable would also see the change:

var myOldVariable = new Object();
var myNewVariable = myOldVariable;
myNewVariable.newChild = 'Hello';
alert(myOldVariable.newChild);
//This will alert 'Hello'

使用函数

The following is an example of using the return statement to return a variable from a function:

function appendComment(passvar) {
  //in this case, I have passed a string variable and I return
  //a string variable. I could return any variable type I want.
  passvar += ' without your help';
  return passvar;
}

var myString = appendComment('I did it');
//myString is now 'I did it without your help'

 

Functions are called using one of these:

nameOfFunction(listOfVariables); 
window.nameOfFunction(listOfVariables); 
object.onEventName = nameOfFunction;

DOM操作

http://www.howtocreate.co.uk/tutorials/javascript/dombasics

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值