NBR-hugh / freecodecamp-practice

Practice everyday
0 stars 0 forks source link

FCC:Object Oriented and Functional Programming (2 hours)面向对象的函数设计法 #16

Open NBR-hugh opened 8 years ago

NBR-hugh commented 8 years ago

2016.10.28 8:30启动

NBR-hugh commented 8 years ago

Declare JavaScript Objects as Variables 将对象设为变量

var motorBike = {
  "wheels":4 ,
  "engines":1,
  "seats":5
};
NBR-hugh commented 8 years ago

Construct JavaScript Objects with Functions 用构造函数创建对象

this

var Car = function() {
  this.wheels = 4;
  this.engines = 1;
  this.seats = 5;
};

呼出构造函数的对象

var Car = function() {
  this.wheels = 4;
  this.engines = 1;
  this.seats = 5;
};

// Only change code below this line.

var myCar = new Car();
myCar.nickname = "xiaobai";

运行结果: image

通过parameters 元素修改对象性质的值

var Car = function(wheels,seats,engines) {
  //Change this constructor
  this.wheels = wheels;
  this.seats = seats;
  this.engines = engines;
};

//Try it out here
var myCar = new Car(8,9,10);
NBR-hugh commented 8 years ago

this

Make Object Properties Private 创建对象的秘密性质

var Car = function() {
  // this is a private variable
  var speed = 10;

  // these are public methods
  this.accelerate = function(change) {
    speed += change;
  };

  this.decelerate = function() {
    speed -= 5;
  };

  this.getSpeed = function() {
    return speed;
  };
};
NBR-hugh commented 8 years ago

Iterate over Arrays with map 用map历遍数组(对数组中所有元素执行同一操作)

var oldArray = [1, 2, 3];
var timesFour = oldArray.map(function(val){
  return val * 4;
});
console.log(timesFour); // returns [4, 8, 12]
console.log(oldArray);  // returns [1, 2, 3]
NBR-hugh commented 8 years ago

Condense arrays with reduce 用reduce压缩数组

The array method reduce is used to iterate through an array and condense it into one value.【将数组中元素压缩成一个数】

var array = [4,5,6,7,8];
var singleVal = 2;

// Only change code below this line.

var singleVal = array.reduce(function(previousVla,currentVla){
  return previousVla + currentVla;
},0);
NBR-hugh commented 8 years ago

Filter Arrays with filter 用fliter过滤数组元素(筛选)

array = array.filter(function(val) {
  return val !== 5;
});
NBR-hugh commented 8 years ago

17

Sort Arrays with sort 用sort排列数组内的元素

var array = [1, 12, 21, 2];
array.sort(function(a, b) {
  return b - a ;
});
NBR-hugh commented 8 years ago

Reverse Arrays with reverse 用reverse对调数组

var array = [1,2,4,3,5,6,7];
var newArray = [];

// Only change code below this line.

newArray = array.reverse();

结果 image

NBR-hugh commented 8 years ago

Concatenate Arrays with concat 用 contat 连接数组

var oldArray = [1,2,3];
var newArray = [];

var concatMe = [4,5,6];

// Only change code below this line.

newArray = oldArray.concat(concatMe);

运行结果
[1,2,3,4,5,6]
NBR-hugh commented 8 years ago

Split Strings with split 用split分开字符串形成数组

var string = "Split me into an array";
var array = [];

// Only change code below this line.

array = string.split(' ');
NBR-hugh commented 8 years ago

Join Strings with join 用join连接数组元素成字符串,split的逆运算

var joinMe = ["Split","me","into","an","array"];
var joinedString = '';

// Only change code below this line.

joinedString = joinMe.join(' ');
NBR-hugh commented 8 years ago

2016.10.28 10:40 END

NBR-hugh commented 8 years ago

TOTAL :2h10min