ผลต่างระหว่างรุ่นของ "01219245/javascript1/tutorial3"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
แถว 6: แถว 6:
  
 
== Quick introduction to objects in JavaScript ==
 
== Quick introduction to objects in JavaScript ==
 +
 +
Objects in JavaScript is a bag that maps between keys and values.  Open a JavaScript console in your browser and try the following example.
 +
 +
We can use curly braces to enclose property names and values to define an object.  To create an "empty" object, we write
 +
 +
var dog = {};
 +
 +
Variable <tt>dog</tt> now points to an object with no properties.  Trying to accessing any of its property will give you ''<tt>undefined</tt>''.
 +
 +
dog.age
 +
 +
We can assign a value to some property in the object with a standard assignment:
 +
 +
dog.age = 10;
 +
dog.color = 'blue';
 +
 +
If you type
 +
 +
dog         
 +
 +
you'll see something like this:
 +
 +
Object { age=10, color="blue"}
  
 
== 21 Game ==
 
== 21 Game ==

รุ่นแก้ไขเมื่อ 01:51, 27 มกราคม 2557

In this tutorial we will learn about objects in JavaScript. We will also develop a Nim game. There are many versions of the game, we will try the version that is usually called the 21 game.

In this game, there are 21 sticks. There are two players: you and the computer. Each player takes turn to take out the sticks. The player can choose between 1 - 3 sticks. The player that picks the last sticks wins.

You can try it at [1].

Quick introduction to objects in JavaScript

Objects in JavaScript is a bag that maps between keys and values. Open a JavaScript console in your browser and try the following example.

We can use curly braces to enclose property names and values to define an object. To create an "empty" object, we write

var dog = {};

Variable dog now points to an object with no properties. Trying to accessing any of its property will give you undefined.

dog.age

We can assign a value to some property in the object with a standard assignment:

dog.age = 10;
dog.color = 'blue';

If you type

dog           

you'll see something like this:

Object { age=10, color="blue"}

21 Game

Picking sticks

Using objects to encapsulate behavior

Transcript

Computer player