1.We can write out anything to the console with:console.log();
2.定义变量:
var strength = '50,000 pounds';
var age = 20;
var hasPet = true;
3.获取字符串长度:
"yourName".length;
4.confirm("I am ready to go.");
These boxes can beused on websites to confirm things with users. You've probablyseen them pop up when you try to delete important things or leave a websitewith unsaved changes.
5. You can ask forinput with a prompt.
Examples:
prompt("What is your name?");
6.=== Equal to
!== Not equalto
7. The number part isa little strange. To select for the "he" in "hello", you would write this:
"hello".substring(0, 2);
8.函数
( 1 定义
var divideByThree= function (number) {
var val = number / 3;
console.log(val);
};
( 2 使用
divideByThree(10);
9.
for (
var i =
1; i <
11; i++) {
/* your code here */;
}
10.数组
var arrayName = [data,data, data];
11.定义object
( 1
var myObject = {
key:
value,
key:
value,
key:
value
};
( 2
var myObj = {
type:
'fancy',
disposition:
'sunny'
};
var myObj =
new Object();
myObj.type =“zzl”;
12.
You can add objects directly to friends, like this:
var friends = {
bill: {},
steve: {}
};
Or with the bracket ([]) or dot(.) notation, like this:
friends[bill] ={};
friends.steve ={};
Or with Object constructors, like this:
var friends =new Object();
friends.bill =new Object();
friends.steve =new Object();
Notice that"bill" and "steve" are not capitalized!
13.add parameters
var friends = {
bill: {
firstName:"Bill",
lastName:"Gates",
number:"(206) 555-5555",
address: ['One Microsoft Way','Redmond','WA','98052']
}
};
1.定义类
var someObj = {
aProperty: value,
someMethod: function(some, params) {}
};
Whenwe call someObj.someMethod(some,values);, thecode between the curly brackets { } willrun.
2.引用内部参数
var james = {
job:"programmer",
married:false,
sayJob:function() {
//complete this method
console.log("Hi, I work as a " + this.job);
}
};
3.
An advantage of bracket notation is that we are not restrictedto just using strings in the brackets. We can also use variables whose valuesare property names:
var someObj = {propName: someValue};
var myProperty =
"propName";
someObj[myProperty];
The last line is exactlythe same as usingsomeObj["propName"];
.
4.获取参数的类型
var someObject = {someProperty: someValue};
console.log(
typeof someObject );
5.
var myObj = {
// finishmyObj
name:"zzl"
};
console.log( myObj.hasOwnProperty('name')); // should print true
console.log( myObj.hasOwnProperty('nickname') ); //should print false
6.
Now let's learn how to work with all the properties that belongto an object. First, let's define an object:
var dog = {
species:
"bulldog",
age:
3,
color: brown
};
To print out all elements, we can use afor/in loop, like this:
for(
var property
in dog) {
console.log(property);
}
In the loop we use console.
log to print out
each key. Rememberthe "property" bit can be any placeholder name you like.
7.打印出类的参数
for(
var x
in dog) {
console.log(dog[x]);
}
8.
In general, if you want to add a method to a class such that allmembers of the class can use it, we use the following syntax to extend the prototype:
className.prototype.newMethod =
function() {
statements;
};
9.构造器
functionPerson(name,age) {
this.name = name;
this.age = age;
};
10.声明继承关系
Set the Penguin
class's prototype to
a new instance of Animal
by adding this line after youmake the constructor:
Penguin.prototype =
new Animal();
11.获取类里的private变量
function Person(first,last,age) {
this.firstname = first;
this.lastname = last;
this.age = age;
var bankBalance = 7500;
this.getBalance= function() {
// your codeshould return the bankBalance
return bankBalance;
};
}
12.
Using constructor notation, aproperty declared as this.property =
"someValue;"will
be public,whereas a property declared with var property=
"hiddenValue;" will
be private.