当前位置:文档之家› Unity3d的Javascript入门教程

Unity3d的Javascript入门教程

Unity3d的Javascript入门教程
Unity3d的Javascript入门教程

2011-10-20 19:04Unity3d的Javascript入门教程Developer

我在奔跑, 我在狂笑,我还很年轻~

VARIABLES (变量)

Let's think of a variable as acontainer that holds something. The Value you set the variable tobe is what it beco mes.

我们来想想,变量就像一个临时存贮某种东西的容器,你给它设置什么值,它就会变成什么

You can choose to name a variable anything you wish, as long as itcontains no spaces, starts with a letter(prefera bly lower case),contains only letters, numbers, or underscores, and is not areserved keyword.

你可以给你的变量命你想要的名字.以小写字母开头.包括使用字母,数字,下划线.

Use the keyword 'var' to create a variable. Let's call our firstone 'box'.

输入"var"可以创建一个变量,让我们调用我们的第一个叫"box"的变量.

Code:(代码)

var box;

There you go; you've declared your first variable! If you'rewondering about the semicolon at the end, statements (commands) inJavascript must end in a semicolon.

Javascript中在申明一个变量时要以";"结尾.

iPhone programmers, if you declare a variable without setting it toa value, you must state what type the variable is, in this caseString. Common types include String, int, float, boolean, andArray.

如果是IPhone程序员,在声明一个变量时一定要指明变量的类型.在这个案例中是字符串型,一般情况下变量类型包括字符串型,整型,布尔型和阵列.

Note that proper capitalization isnecessary!

var box :String;

Of course, our box is empty, so let's set it to a value by addingthe following line:

当然我们的box是空的,因此我增加以代码给它一个值.

Code:

box ="apple";

Now our box contains a text string (variable type), which happensto be "apple".

现在我们的box包含字符型.它叫"apple".

Code:

var box ="apple";

But once it's declared, you can't declare itagain.

但是你一旦申明一个变量就不能重复申明同一个变量

You can, however, change it to another value (as long as the newvalue is the same type as the original).

那么你如何改变成另一个值呢?

Code:

box ="orange";

In addition to text strings, variables can hold numbers:

除了字符串,你也可以存贮数字类型

Code:

var number =10;

This is an integer (int), which means whole numbers only... nodecimal places.

这是整型(用int表示),它是个整数类型,没有小数点.

But we could also do a floating point number (float), which hasdecimal places:

我们也能用浮点型(用float表示)

Code:

var myFloat =10.5;

Variables can also contain booleans. A boolean is simply atrue/false value:

变量也能包括布尔型(用boolean表示), 布尔型只有true/false(真或假)两个值.

Code:

var gameOver =true;

We'll discuss these more later.

稍后我们会有更多的讨论.

Notice that a variable can normally only contain one thing at atime. But we can make a variable that contains m ultiple things, bycreating an array:

注意,通常情况下一个变量只包含一种东西,我们也可以让一个变量包含更多的内容我们就通过创建一个阵列来实现. Code:

var applePie = Array("apple", "brown sugar","butter", "pie crust");

This variable contains everything you need to make an applepie!

这个变量包含了制作一个的所以有东西

If we wanted to view the contents of applePie, we could output itto the Unity console using the Debug comman d.

如果我们想看看苹果派内容,可以通过Debug命令将它们输出到Unity控制台.

Add this line to the code above:

On play, the console should display: apple,brownsugar,butter,pie crust

按下播放钮,控制台将显示:apple,brown sugar,butter,piecrust

To access a single element (item) from an array, we can access itthru its index (position in array). This may seem c onfusing, butindexes start at 0. So index 0 is actually the first element in thearray.

我们可以通过它的序号来访问它的元素,这看起来似乎有点混乱,不过由于序号是从0开始的,实际上0是阵列的第一个元素. Code:

var applePie = Array("apple", "brown sugar","butter", "pie crust");

var item1 = applePie[0];

Debug.Log(item1);

You can actually use indexing with Strings as well. The followingcode displays the first character of "hello", thelet ter 'h':

实际上你也可以用索引序号来访问字符.比如下面代码就显示"hello"的第一个字母"h"。

Code:

var myString ="hello";

Debug.Log(myString[0]);

Now lets alter our variables in other ways.

现在我们来改变一下我们的变量

We can add variables:

Code:

var number1 = 10;

var number2 = 11;

var total = number1 + number2;

Debug.Log(total);

If you add an int (integer) to a float (floating point number) theresult becomes a float:

如果我们给一个整型变量增加一个浮点型,结果就会变成一个浮点型变量

Code:

var number1 = 100;

var total = number1 + 1.5;

Debug.Log(total);

Obviously, we can also subtract/divide/multiply ints andfloats.

显然我们也可以用减/除/剩整型和浮点型变量

Less obvious, we can also 'add' strings. This merges themtogether:

可以通过“add”(加)这个字符串把它们结合在一起。

Code:

var string1 ="apple";

If we add a number to a string the result becomes astring.

如果我们把一个数字加到一个字符串类型上结果也会变成字符串型。

We can also multiply strings.

我们还可以对字符串使用剩的方式,使它倍增。

Code:

var greeting ="howdy";

Debug.Log(greeting * 3);

Unfortunately, we can't divide or subtract strings. There are waysto split strings and remove parts of strings, but t hat is a moreadvanced topic.

不幸的事,我们不能对字符串型进行减或除的操作,有专门对字符串进行分离和删除的方法,当然这是高级话题。

Let's wrap up our discussion on variables with incrementing numbers(changing their value by a set amount). 下面我们来看看怎样让数值递增

First, declare a variable and set it to 1:

首先申明一个变量,设置它的值为了:

Code:

var number =1;

We can increment numbers various ways.

我们能用不同的方式来增加这个数

Code:

number = number +1;

The above adds 1 to our number, and it becomes2.

上面是对原有的数加上1.其结果变成2.

But programmers are lazy, and decided this was too long. So theyabbreviated it to:

可是程序员可都要省事,可能这样定觉得太长了,所以要写短一点

Code:

number +=1;

This is simply shorthand for number = number +1;

可以简单地缩短成number=number+1;

But guess what? Lazy programmers decided even THIS was too long,and shortened it to:

可是你猜怎么着,懒惰的程序员还是觉得它太长了.于是就缩短成这样:

Code:

number++;

a value other than 1,++ won't work. ++ is shorthand for += 1only.

这些方法的结果都是一样的,但值得注意的事增加指定的值"++"是不可以的."++"只表示"+=1"

You can also do the same with subtraction:

同样道理,你也可以写成"--"

Code:

number--;

But for division and multiplication, you must use one of the longerforms:

但是乘除就不能那样了

Code:

number = number/2;

number *= 2;

Note that an asterisk means multiply, a slash means divide.

Enumeration (枚举)

Enum allows you to create a collection of integer based constantvalues. While working with Javascript variables i nstead of creatingindividual variable for constants you must use Javascript enumvariable to store integer based v alues.

Enum(枚举数型)允许你创建一个整数集合.

Javascript enum type variables and their valuesprovide the systematic code pattern as well as readability andund erstanding for the code. You can easily get the integer valueassociated with the named enum type item.

JS的枚举类变量和它们的值提供了代码模式,以便它的可读性和对代码的理解.你可以轻易地一个整数值

Code:

enum myEnum { myName1, myName2, myName3}

Enum is an enumeration type collection that stores the items withcomma separation "," and its corresponding integer value separated with colon ":".

Code:

enum AiState { Sleeping, Idling, Chasing,Dying }

function Update ()

{

varstate :AiState; //first example

var state = AiState.Sleeping;

print (state);

var curState :AiState; // secondexample

curState = AiState.Idling;

switch (curState)

{

case AiState.Sleeping: print ("aiState issleeping"); break;

}

Note: The Enumeration should be declared outside a function

注意:枚举类应该在函数外面申明.

IF STATEMENTS (if语句)

If statements are conditional statements. If a condition evaluatesas true, do something.

We can do a comparison of two values by using two equal signs,==

"number == 10" evaluates as true if our number variable equals 10,otherwise it evaluates as false.

if语句是条件语句,如果条件满足为真,执行.我们可以用"=="双等号对两个值进行比较,比如"number==10"当条件为真时变量number等于10,反之为假.

Note: it's important to remember to use two equal signs whencomparing variables/values, but one equal sign wh en setting avariable to a value!

注意.重要的事要记住比较变量与值时用双等号,而不是一个等号.

The following creates a variable and sets it to true, checks to seeif the variable equals true, and if so prints a text 下面创建一个变量,设置为真,检测为真时打印后面的文字.

string to the console:

Code:

var gameStarted =true;

if (gameStarted == true)

Debug.Log("Game hasstarted");

The above is actually redundant, since our variable 'gameStarted'is a boolean. There is no reason to check "if tru e equals true",just check "if true":

上面这些是多此一举,由于我们的变量gameStarted是布尔型,没理由检测是否为"等于'=='"真

Code:

var gameStarted =true;

if (gameStarted)

Debug.Log("Game hasstarted");

If you're wondering why I didn't put a semicolon behind if(gameStarted), it's because technically it is only the fir st halfof the statement. I could have written it like so:

你可能觉得奇怪.为什么我不在if后面放上";"分号呢,那是因为理论上说它只是语句的第一部分.我可以写成这样:

Code:

if (gameStarted) Debug.Log("Game hasstarted");

I could have also written it this way:

if (gameStarted){

Debug.Log("Game hasstarted");

}

Those brackets represent a block of code, and tell the if statementto execute anything in between... if the conditi on istrue!

花括号代表一个代码模块,告诉if语句如条件为真则执行模块的代码.

When if contains only one statement to execute, the brackets areoptional. But if it contains more than one state ment, you MUST usethe brackets! Note that semicolons are not needed afterbrackets

当if条件句下只有一行代码,花括号可以不要,但如果是多行就是必须的.

Code:

var gameStarted =false;

If (gameStarted == false){

gameStarted =true;

Debug.Log("I just started thegame");

}

Read the second line of code above. Remember those lazyprogrammers? They don't want to write

Code:

if (gameStarted == false)

阅读以上代码,记得那些懒惰的程序员吗,他们不想这样写代码:if (gameStarted == false)

When they can just write:

他们这样写:

Code:

If (notgameStarted)

But you know what? Why write 'not' when I can shorten that too?

还可以这样写:

Code:

if (!gameStarted)

Yes, an exclamation point means 'not' to lazyprogrammers!

好吧,一个惊叹号对于一个懒惰的程序来说它的意思是"否"

You can also combine this with equals, where it means "notequals":

你也可以把它与"="号合在起用,表示"不等于"

Code:

var answer = 1;

You can also check for greater than or less than:

你也能检测更大或更小的数:

Code:

var age = 18;

if (age > 18)

Debug.Log("oldenough");

else if (age < 18)

Debug.Log("jailbait");

else

Debug.Log("exactly18");

Notice the 'else if' and 'else' keywords? if the first if statementcondition fails (evaluates as false), it then checks t he conditionunder else if. If that one fails, it will check the next else if(if one is available), and finally if all conditi ons fail, itexecutes the statement under else. Again, if the 'if', 'else if',or 'else' statements contain more than one statement, each block ofcode must be separated by brackets.

注意:"else if"和"else"关键词.如果第一个条件失败(执行为假),那么它就检测elseif下的条件,如果还是失败的,就执行else下面的条件,依此往复."if","elseif","else"语句可以包括多个语句,每个代码模块都须由花括号分开.

You can also check for multiple conditions in a singlestatement:

你也可以在一条语句中检测多个条件:

Code:

if (age >= 21&& sex =="female")

buyDrink =true;

Above, we introduced greater than or equal to >= andthe AND operator, which is two ampersand characters:&&.上面我对多个条件用>=及&&表和

If both conditions are true, the statement is executed. If even oneis false, the statement is not.

如果两个条件都为真,语句执行,如果有一个为假,则语句不执行

Note: if you want to run the above code, remember to createvariables for age (int), sex (String), and buyDrink (boolean) first!

6

Code:

if (engine == "Unity" || developer =="friend")

buyGame = true;

Above, we used the OR operator, which is two pipe characters: ||.If either condition is true, the statement is executed. If both are false, the statement isnot.

Note: if you want to run the above code, remember to createvariables for engine (String), developer (String), and 推荐

If can also be used with the keyword 'in'. This is generally usedwith Arrays:

Code:

var names = Array("max", "rick", "joe");

if ("joe" in names) Debug.Log("Found Joe!");

SWITCH / CASE STATEMENTS

These statements allow you to take a single variable and performmultiple operations depending upon what that variable

contains. Here is a simple example:

Code:

switch(somevariable)

{

case"A":

somefunction();

break;

case"B":

someotherfunction();

break;

default:

break;

}

The switch statement above takes a variable called "somevariable"and performs one of three events depending upon the value

of the variable. If the variable contains A then the somefunction()function will be called. If the variable contains B, then the

someotherfunction() function will be called, otherwise the defaultoperation is used. The break statement after ea ch case will

stop the script at that point, so cases are useful for specificscenarios.

7

LOOPING

Looping allows you to repeat commands a certain amount of times,usually until some condition is met.

What if you wanted to increment a number and display the results tothe console?

var number = 0;

number += 1;

Debug.Log(number);

number += 1;

Debug.Log(number);

number += 1;

Debug.Log(number);

And so on... but this is redundant, and there is nothing lazyprogrammers hate more than rewriting the same cod e

over and over!

So use a For Loop:

Code:

var number = 0;

for (i=1; i<=10; i++){

number +=1;

Debug.Log(number);

}

Okay, that for statement on the second line may look a littleconfusing. But it's pretty simple actually.

i=1 -created a temporary variable i and set it to 1. Note that youdon't need to use var to declare it, it'simplied. i<=10 -how long to run the loop. In that case,continue to run while i is less than or equal to10.

i++ -how to increment loop. In this case, we are incrementing by 1,so we use the i++, shorthand for i+=1

If we're just printing 1 thru 10, our code above could beshortened. We don't really need the number variable: Code:

for (i=1; i<=10; i++)

Debug.Log(i);

Just like if statements, brackets are optional when there is onlyone statement to execute. Talk about beating a dead horse...

We can also count backwards:

Code:

for (i=10; i>0; i--)

Debug.Log(i);

Or print all even numbers between 1 and 10:

Code:

for (i=2; i<=10; i+=2)

Debug.Log(i);

8

While executes repeatedly until a condition is true.

Code:

var number = 0;

while (number < 10){

number++;

Debug.Log(number);

}

While loops are most useful when used with booleans. Just make surethe escape condition is eventually met, or you'll be stuck in an infinite loop and the game will most likelycrash!

Code:

var playerJumping = true;

var counter = 0;

while (playerJumping){

//do jumpstuff

counter +=1;

if (counter >100) playerJumping = false;

}

Debug.Log("While loop ended");

Notice the fourth line of code above? The one that starts with twoslashes? This means the text afterwards is a comment, and will not be executed. Comments are useful for notinghow your code works for yourself or others, for putting in placeholder text to be replaced later (as above), orfor commenting out sections of your code for debug purposes.

9

FUNCTIONS

If you thought loops were a time saver, wait until you find outabout functions!

Functions allow you to execute a whole bunch of statements in asingle command.

But lets keep things simple at first. Lets define (create) afunction that simply displays "Hello world" on the console.

Code:

function SayHello(){

Debug.Log("Helloworld");

To execute, or 'call' this function, simply type:

Code:

SayHello();

Note the parenthesis after our function. They are required, bothwhen we define our function and call it. Also not e

that our function name is capitalized. It doesn't have to be, butcapitalizing function names is the norm inUnity.

What if we wanted a function to say different things? We can pass avalue, or argument, to the function: Code:

function Say(text){

Debug.Log(text);

}

Above, text is the argument, a temporary variable that can be usedwithin the function only.

iPhone programmers, you should also state what type the argumentis, in this case String.

function Say(text : String){

Now when we call our function, we have to provide anargument:

Code:

Say("Functions are cool!");

We can also pass variables:

Code:

var mytext = "I want a pizza";

Say(mytext);

Another useful thing functions can do is return a value. Thefollowing function checks to see if a number is even and if so it returns true, else it returns false:

Code:

function EvenNumber(number){ //iPhone programmers, remember to addtype to arg (number : int);

if (number % 2 ==0)

10

// NOTE: % is the mod operator. It gets the remainder of numberdivided by 2

return true;

else

return false;

}

var num = 10;

if ( EvenNumber(num) )

Debug.Log("Number " + num + "is even");

don't have to return a value:

Code:

function Update(){

if (!gameStarted) return;//exit function

}

The Update() function above is one of the main functions in Unity.You do not have to call it manually; it gets call ed

automatically every frame.

OVERLOADING FUNCTIONS

Functions can be overloaded. Sounds complicated, but it's reallyquite simple. It means you can have multiple versions of a function that handles different types of arguments,or different numbers of arguments.

To handle different types of arguments, simply use the colon : tostate the type of argument being passed. Common types include String, int, float, boolean, and Array. Notethat proper capitalization is necessary! Code:

function PrintType(item : String){

Debug.Log("I'm a string, typeString");

}

function PrintType(item : int){

Debug.Log("I'm an integer,type int");

}

function PrintType(item : float){

Debug.Log("I'm a float, typefloat");

}

function PrintType(item : boolean){

Debug.Log("I'm a boolean, typeboolean");

}

function PrintType(item : Array){

Debug.Log("I'm an array, typeArray");

}

function PrintType(item: GameObject){ //catches everythingelse

Debug.Log("I'm somethingelse");

}

function PrintType(){

Debug.Log("You forgot tosupply an argument!");

PrintType();

PrintType("hello");

PrintType(true);

12

CLASSES

So variables have different types, such as String and int. But whatif you need a new type that does something different?

Classes are simply new types that YOU create.

Code:

class Person{

varname;

varcareer;

}

//Create objects of type Person

var john = Person();

https://www.doczj.com/doc/a01063141.html, = "John Smith";

john.career = "doctor";

Debug.Log(https://www.doczj.com/doc/a01063141.html, + " is a " + john.career);

The above class has two class variables, or properties, name andcareer. You access them by typing the name of the instance (in this case, John) followed by a period and the nameof the property.

You can also pass arguments when creating instances of a class. Youdo this by creating a constructor, which is a special function that is automatically called whenever a newinstance of your class is created. This function has th e

same name as your class, and is used to initialize the class:

Code:

class Animal{

var name;

function Animal(n : String){ //this is the constructor

name = n;

Debug.Log(name + " was born!");

}

}

cat = Animal("Whiskers"); //varkeyword is optional when creating instances!

This is useful for having instances interact with oneanother:

Code:

class Person{

var name :String;

function Person(n :String){

name = n;

}

function kiss(p :Person){

Debug.Log(name + " kissed " + https://www.doczj.com/doc/a01063141.html, +"!");

}

}

jenni = Person("Jenni");

bob = Person("Bob");

jenni.kiss(bob);

13

INHERITANCE

Classes can inherit or extend (add functionality to) another class.The class that gets inherited from is usually calle d

the base class or the parent class. The extended class is alsocalled the child class or the derivedclass.

This will be our base class:

Code:

class Person{

var name :String;

function Person(n : String){//constructor

name = n;

}

function Walk(){ //classfunction

Debug.Log(name + " is walking");

}

}

To extend this class, create a new class with the keyword'extends':

Code:

class Woman extends Person{

var sex :String;

function Woman(n : String){//constructor

super(n); //calls the original constructor andsets name

sex = "female"; //adds additional functionality to the extendedclass

}

functionWalk(){

super.Walk(); //calls the original function

Debug.Log("And she is so sexy!"); //adds additional functionalityto the extended class

Note that we can access the base/parent class properties andfunctions by using the keyword 'super'.

If we create an instance of Woman and call function Walk(), boththe parent and child function are called: Code:

amanda = Woman("Amanda");

amanda.Walk();

14

BUILT IN TYPES ANDPROPERTIES

Now you're probably wondering, "if classes, the types I create, canhave properties and functions, why can't the built in types"?

They do, actually.

To convert an int to a String, use the built-in functionToString():

Code:

var number = 10;

var text = number.ToString();

To get the length of an Array (or a String), use the built-inproperty length:

Code:

var animals = Array("cat", "dog", "pig", "dolphin","chimpanzee");

var total = animals.length;

You can use this in a for loop. Add the following two lines to thecode above:

Code:

for (i=0; i

Debug.Log(animals[i]);

}

This displays the contents of our array, one item at atime.

To add an item to an Array, use the function Add():

Code:

animals.Add("lemur");

To split a String into an array, use the function Split():

Code:

Debug.Log(Array(words));

To get a part of a String, use the function Substring(). Substringtakes two arguments, the starting index and the ending index:

Code:

var sentence = "The quick brown fox jumped over the lazydog";

var firstHalf = sentence.Substring(0, 19);

Debug.Log(firstHalf);

To capitalize a text string, use the function ToUpper();

Code:

var mission = "go make cool games!";

Debug.Log( mission.ToUpper() );

15

As you might expect, there is also a ToLower() function.

Code:

Debug.Log( ("THE END").ToLower() );

ADDITIONAL INFORMATION

Intro to Scripting with Unity

https://www.doczj.com/doc/a01063141.html,/support/Tutorials/2 - ScriptingTutorial.pdf

Intro to Unity Programming on Unify Wiki

https://www.doczj.com/doc/a01063141.html,/wiki/index.php?title=Programming_Chapter_1_Old

Unity Script Reference Page

https://www.doczj.com/doc/a01063141.html,/support/documentation/ScriptReference/

MSDN - This is for advanced programmers. Search for JSCRIPT, asit's quite similar to Unity javascript.

https://www.doczj.com/doc/a01063141.html,/en-us/library/aa187916.aspx

_________________

Froggy Math

https://www.doczj.com/doc/a01063141.html,/~torajima/meganesoft

_________________

Difference between standard java and unity java script

https://www.doczj.com/doc/a01063141.html,/wiki/index.php?title=Head_First_into_Unity_with_JavaScript

浏览(718)评论转载

评论

发布

帮助中心 | 空间客服 | 投诉中心 | 空间协议

?2012 Baidu

JavaScript入门教程(初学者不可多得的优秀入门教材,通俗易懂,专业术语通俗化)

第 1 章 JavaScript 语言入门 1 为什么学习 JavaScript
提要:Javascript 是学习脚本语言的首选。她兼容性好,绝大多数浏览器均支持 Javascript,而且她功能强大,实现简单方便,入门简单,即使是程序设计新手也可以非常 快速容易地使用 JavaScript 进行简单的编程。
Javascript 是由 Netscape 公司创造的一种脚本语言。为便于推广,被定为 javascript,但 是 javascript 与 java 是两门不相干的语言, 作用也不一样。 作为一门独立的编程语言, javascript 可以做很多的事情,但它最主流的应用还是在 Web 上——创建动态网页(即网页特效)。 Javascript 在网络上应用广泛, 几乎所有的动态网页里都能找到它的身影。 目前流行的 AJAX 也是依赖于 Javascript 而存在的。 Javascript 与 Jscript 也不是一门相同的语言, Jscript 和 vbscript 是微软开发的两种脚本语 言,微软,Netscape 公司以及其他语言开发商为减少 web 开发者的兼容麻烦,所以成立 ECMA , 该组 织 专 门制定 脚 本 语 言的 标 准 和规范 。 ECMA 制 定 的标 准脚 本 语 言 叫做 ECMAScript,Javascript 符合 ECMA 的标准,其实 Javascript 也可以叫做 ECMAScript. Jscript 也 ECMA 的标准, 但用户较少。vbscript 仅局限在微软的用户, Netscape 不支持。 概括地说,JavaScript 就是一种基于对象和事件驱动,并具有安全性能的脚本语言,脚 本语言简单理解就是在客户端的浏览器就可以互动响应处理程序的语言, 而不需要服务器的 处理和响应,当然 JavaScript 也可以做到与服务器的交互响应,而且功能也很强大。而相对 的服务器语言像 asp https://www.doczj.com/doc/a01063141.html, php jsp 等需要将命令上传服务器,由服务器处理后回传处理结 果。对象和事件是 JavaScript 的两个核心。 JavaScript 可以被嵌入到 HTML 文件中,不需要经过 Web 服务器就可以对用户操作作 出响应,使网页更好地与用户交互;在利用客户端个人电脑性能资源的同时,适当减小服务 器端的压力,并减少用户等待时间。
2 将 JavaScript 插入网页的方法
与在网页中插入 CSS 的方式相似,使用
language="javascript"表示使用 JavaScript 脚本语言,脚本语言还有 vbscript、 jsscript 等,如果没有 language 属性,表示默认使用 JavaScript 脚本。其中的...就是代 码的内容。例如:

Javascript基础教程

Javascript简介 (2) Javascript简介 (2) 简单的Javascript入门示例 (4) 编写Javascript 代码 (5) 语句(Statements) (5) 语句块(Blocks) (6) 注释(Comments) (7) 表达式(Expressions) (8) 赋值和等于(Assignments and Equality) (9) Javascript常用运算符(Operators) (10) 算术运算符 (10) 逻辑运算符 (11) 赋值运算符 (12) Javascript 循环语句(Javascript Loop Statements) (12) 使用for 循环语句 (13) 使用for...in 循环语句 . (15) 使用while 和do...while 循环语句 (17) 使用break 和continue 语句 (20) Javascript写在哪里 (23) Javascript在之间 (23)

Javascript在之间 (24) Javascript放在外部文件里 (25) Javascript变量(Javascript Variables) (26) 什么是变量? (26) 变量的声明(Declaring Variables) (26) 变量的命名规则 (27) Javascript条件语句(Javascript Conditional Statements) (27) 单项条件结构(if条件语句) (28) 双向条件结构(if...else条件语句) (29) 多项条件结构(switch条件语句) (31) Javascript保留字(Javascript Reserved Words) (32) Javascript未来保留字(Javascript Future Reserved Words) (33) Javascript简介 Javascript简介

javascript基础练习题

基础练习题 一、简单Java程序调试 1)以下哪个是Java应用程序main方法的有效定义? A. public static void main(); B. public static void main( String args ); C. public static void main( String args[] ); D. public static void main( Graphics g ); E. public static boolean main( String a[] ); 2) 编译和运行以下代码的结果为: public class MyMain{ public static void main(String argv){ System.out.println("Hello cruel world"); } } A.编译错误; B.运行输出"Hello cruel world"; C.编译无错,但运行时指示没有定义构造方法。 D.编译无错,但运行时指示没有正确定义main方法。 3)下列选项中不属于Java虚拟机的执行特点的一项是: A.异常处理B.多线程C.动态链接D.简单易学 4)不属于Java语言特点的一项是: A.分布式 B. 安全性 C. 编译执行 D.面向对象 5)以下程序的运行结果为: public class Test{ public static void main(String argv[ ]){ System.out.println("x="+5); } } A. 5 B. x=5 C. "x="+5 D. "x="5 6) 以下程序的运行结果为: public class Test{ public static void main(String argv[ ]){ System.out.println("good"+"morning"); } } A. goodmorning B. "good"+"morning" C. good morning D. good+morning 二、Java符号与表达式 1) 现有一个int类型的整数和一个double类型的数进行加法运算,则得到的结果类型为: A.int类型 B. double类型 C. float类型 D. long类型 2)下面程序段的输出结果是:

最新JavaScript_深度剖析(从入门到精通)

第一讲JavaScript语言概况 第二讲JavaScript基本数据结构 第三讲JavaScript程序构成 第四讲基于对象的JavaScript语言 第五讲创建新对象 第六讲使用内部对象系统 第七讲窗口及输入输出 第八讲WEB页面信息的交互 第九讲实现更复杂的交互 第一讲JavaScript语言概况 Internet时代,造就了我们新的工作和生活方式,其互联性、开放性和共享信息的模式,打破了传统信息传播方式的重重壁垒,为我们带来了新的机遇。随着计算机和信息时代的到来,人类社会前进的脚步在逐渐加快,每一天都有新的事情发生,每一天都在创造着奇迹。随着Internet技术的突飞猛进,各行各业都在加入Internet的行业中来。无论从管理方面,还是从商业角度来看,Internet都可以带来无限生机。通过Internet,可以实现地区、集体乃至个人的连接,从而达到一种“统一的和谐”。那么怎样把自己的或公司的信息资源加入到WWW 服务器,是广大用户日益关心的问题。采用超链技术(超文本和超媒体技术)是实现这个目标最简单的、最快速的手段和途径。具体实现这种手段的支持环境,那就是HTML 超文本标识语言。通过它们可制作所需的Web网页。 通过超文本(Hyper Text)和超媒体(Hyper Media)技术结合超链接(Hyper link)的链接功能将各种信息组织成网络结构(web),构成网络文档(Document),实现Internet上的“漫游”。通过HTML符号的描述就可以实现文字、表格、声音、图像、动画等多媒体信息的检索。 然而采用这种超链技术存在有一定的缺陷,那就是它只能提供一种静态的信息资源,缺少动态的客户端与服务器端的交互。虽然可通过CGI (Common Gateway Interface)通用网关接口实现一定的交互,但由于该方法编程较为复杂,因而在一段时间防碍了Internet技术的发展。而JavaScript的出现,无凝为Internet 网上用户带来了一线生机。可以这样说,JavaScript的出现是时代的需求,是当今的信息时代造就了JavaScript。 JavaScript的出现,它可以使得信息和用户之间不仅只是一种显示和浏览的关系,而是实现了一种实时的、动态的、可交式的表达能力。从而基于CGI静态的HTML页面将被可提供动态实时信息,并对客户操作进行反应的Web页面的取代。JavaScript脚本正是满足这种需求而产生的语言。它深受广泛用户的喜爱的

JAVASCRIPT从入门到精通读书笔记

《JavaScript从入门到精通》读书笔记 今年的学习计划要读的书是《JavaScript权威指南》,里面内容比较有深度,所以决定买了比较好理解的《JavaScript从入门到精通》开始学习。 此书前半部分还是比较基础,平时工作中都能用得到,后半部分涉及到了Ajax、本地数据存储、离线应用和canvas图形等比较高级的用法。 首先主要介绍了JavaScript的发展历史版本变化,了解一下就可以。 初次使用JavaScript,重点讲了JavaScript的”字符串。例如。浏览器在加载如下代码就会产生一个错误。”); } Hi(); 错误原因:当浏览器解析到字符串””时,会结束JavaScript代码段的执行。解决方法: 使用转义字符把字符串‘’分成两部分来写就不会造成浏览器的误解。 代码测试和错误处理,理解浏览器的不同内核和代码不同的兼容性,在不同浏览器代码报错的时候会有不同的调试方法,学会使用浏览器的调试器对网页开发效率会有很大的提高。现在主流的浏览器是Chrome、Firefox、Safari等。 JavaScript的基本语法和各种变量,各种数据类型及各种数据类型的转换。 重点:避免变量污染 Var foo = function(){ Var a = 1, b = 2; Var bar = function(){ Var b = 3, c=4, //a= 1,b =3, c=4 a+=b + c; // a=8, b=3, c=4 }; //a=1, b=2, c = undefined bar(); //a= 21,b=2,c= undefined } JavaScript运算符的使用。JavaScript定义了51个运算符,主要分为一下几大类,位运算符、算术运算符、逻辑运算符、关系运算符、赋值运算符、对象炒作运算符和其他运算符。设计程序结构。程序都是由一个或多个语句组成的集合,语句表示一个可以执行的命令。用来完成特定的任务。大部分语句用于流程控制,在JavaScript中提供了if条件判断语句、switch多分枝语句、for循环语句、while循环语句、do/while循环语句、break语句、continue语句等7种流行控制语句。

JavaScript API入门指导

JavaScript API入门指导 天地图有限公司 2014年1月

Web API介绍 天地图Web API是一套由JavaScript语言编写的应用程序接口,它能够帮助您在网站中制作各种类型、行业的地图应用,还可以使地图功能够以模块化集成在不同类型的系统应用中。 面向用户 天地图Web API面向的读者是有一定JavaScript编程经验的读者,此外,读者还应该对地图产品有一定的了解。初级程序员通过1-2天的学习,即可掌握API的使用。 获取API 地图API是由JavaScript语言编写的,您在使用之前需要通过之间。

之间的是文本类型(text),ja vascript是为了告诉浏览器里面的文本是属于JavaScript语言。 我也可以独立(引用JS外部文件) 通过前面知识学习,我们知道使用,你可以在 HTML 文档的任意地方插入 JavaScript,甚至在之前插入也不成问题。不过如果要在声明框架的网页(框架网页)中插入,就一定要在之前插入,否则不会运行。 基本格式