1.js定义对象方法
方法一
var car = new Object();
car.colour = 'red';
car.wheels = 4;
car.hubcaps = 'spinning';
car.age = 4;
方法二
var car = {
colour:'red',
wheels:4,
hubcaps:'spinning',
age:4
}
2.js定义数组方法
方法一
var moviesThatNeedBetterWriters = new Array(
'Transformers','Transformers2','Avatar','IndianaJones 4'
);
方法二
var moviesThatNeedBetterWriters = [
'Transformers','Transformers2','Avatar','IndianaJones 4'
];
方法三
var car = new Array();
car['colour'] = 'red';
car['wheels'] = 4;
car['hubcaps'] = 'spinning';
car['age'] = 4;
3.if条件用法
方法一
var direction;
if(x < 200){
direction = 1;
} else {
direction = -1;
}
方法二:当条件为true 时取问号后面的值,否则取冒号后面的值。
var direction = x < 200 ? 1 : -1;