Free Code Camp 练习总结(二) —— JavaScript 基本知识 Object Oriented and Functional Progra

我会每天都更新我写东西,学大概有一段时间了,我感触还是挺大的,把之前基础知识又复习一遍,活到老学到老。希望能给在FreecodeCamp有同学有帮助,我会每天更新我在写FreecodeCamp,代码。

现在让我们开始学习最流行的JavaScript库jQuery吧,不用担心JavaScript本身,我们稍后会提到它,我现在开始接上面Basic front End Development Proiects 继续更新内容,希望跟大家学习交流。

#239 Declare JavaScript Objects as Variables

在我们深入面向对象编程之前,让我们先回顾一下Javascript 的对象(Object)

var motorBike = {

  // 只能在这一行下面写代码
  "wheels":8,
  "engines":6,
  "seats":9

};

#240  Construct JavaScript Objects with Functions

除了上一种方法外,我们还可以使用构造函数来创建对,构造函数通常使用大写字母开头,以便把自己和其他普通函数区别开。

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

#241  Make Instances of Objects with a Constructor Function

我们写的构造函数在这里用起来:

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

// 只能在这一行下面写代码

var myCar = new Car();

myCar.nickname  = "youyouxiong";

#242  Make Unique Objects by Passing Parameters to our Constructor

为了解决这个问题,我们要向构造函数添加参数。

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(3,1,2) ;
#243  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;
  };
};

var Bike = function() {

  // 只能在这一行下面写代码
  var gear;
  this.setGear = function(num){
    gear = num; 
  };
  this.getGear = function()
  {
    return gear;
  };

};

var myCar = new Car();

var myBike = new Bike();
私有变量gear,通过对象属性的方式不能访问,通过setGear方法来访问

#244 Iterate over Arrays with map

map方法会迭代数组中的每一个元素,并根据回调函数处理每一个元素,最后返回一个新数组,注意这个方法不改变原始数组。

使用 map 方法来为 oldArray 中的每一项增加3,并且在 newArray 中保存它们。 oldArray不应该被改变。

var oldArray = [1,2,3,4,5];

// 只能在这一行下面写代码

var newArray = oldArray.map(function(val){
      return val +=3;
    });
#245 Condense arrays with reduce

数组方法reduce 用来迭代一个数组,并且把它累计到一个值中。

使用reduce方法是,你要传入一个回调函数,这个回调函数的参数是一个累加器(比如例子中的previousVal)和当前值(currentVal).

reduce方法有一个可选的第二参数,它可以被用来设置累加器的初始值。如果每天在这定义初始值,那么初始值将变成数组中低第一项,而currentVal将从数组的第二项开始。

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

// 只能在这一行下面写代码

 singleVal = array.reduce(function(previousVal,currentVal){
  return previousVal+currentVal;
},0);
#246  Filter Arrays with filter

fiter 方法用来迭代一个数组,并且按给出的条件过滤出符合的元素。

filter方法传入一个回函数,这个回调函数会携带一个参数,参数为当前迭代的项(我们叫它val)

回调函数返回true的项保留在数组中,返回false的项会被过滤出数组。

var oldArray = [1,2,3,4,5,6,7,8,9,10];

// 只能在这一行下面写代码

var newArray = oldArray.filter(function(val){
  return val <6;
});
#247  Sort Arrays with sort

 使用sort方法,你可以很容易的按照字母顺序或者数字顺序对数组中的元素进行排序。

与我们之前用的数组方法不仅仅返回一个新数组不同,sort方法将改变原数组,返回被排序后的数组。

 

sort 可以把比较函数作为参数传入。比较函数有返回值,当 a 小于 b,返回一个负数;当 a大于 b ,返回一个正数;相等时返回0。

如果没有传入比较函数,它将把值全部转成字符串,并按照字母顺序进行排序。

var array = [1, 12, 21, 2];

// 只能在这一行下面写代码

array.sort(function(a,b){
  return b -a ;
});
#248 Reverse Arrays with reverse

可以使用reverse方法来翻转数组。

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

// 只能在这一行下面写代码

newArray = array.reverse();
#249  Concatenate Arrays with concat

concat 方法可以用来把两个数组的内容合并到一个数组中。

concat方法的参数应该是一个数组。参数中的数组拼接在原数组的后面,并作为一个新数组返回。

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

var concatMe = [4,5,6];

// 只能在这一行下面写代码

newArray = oldArray.concat(concatMe);
#250  Split Strings with split

你可以使用split方法按指定分隔符将字符串分割数组。

你要给split方法传递一个参数,这个参数将会作为一个分隔符。

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

// 只能在这一行下面写代码

array = string.split(" ");

#251 Join Strings with join

我们还可以使用Join 方法来把数组转换字符串,里面的每一个元素可以用你指定的连接符连接起来,这个连接符就是你要传入的参数。

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

// 只能在这一行下面写代码

joinedString = joinMe.join(" ");
 

Build sophisticated web applications by mastering the art of Object-Oriented Javascript About This Book Learn popular Object-Oriented programming (OOP) principles and design patterns to build robust apps Implement Object-Oriented concepts in a wide range of frontend architectures Capture objects from real-world elements and create object-oriented code that represents them Learn the latest ES6 features and how to test and debug issues with JavaScript code using various modern mechanisms Who This Book Is For JavaScript developers looking to enhance their web developments skills by learning object-oriented programming. What You Will Learn Get acquainted with the basics of JavaScript language constructs along with object-oriented programming and its application. Learn to build scalable server application in JavaScript using Node.js Generate instances in three programming languages: Python, JavaScript, and C# Work with a combination of access modifiers, prefixes, properties, fields, attributes, and local variables to encapsulate and hide data Master DOM manipulation, cross-browser strategies, and ES6 Identify and apply the most common design patterns such as Singleton, Factory, Observer, Model-View-Controller, and Mediator Patterns Design applications using a modular architecture based on SOLID principles In Detail JavaScript is the behavior, the third pillar in today's paradigm that looks at web pages as something that consists of : content (HTML), presentation (CSS), and behavior (JavaScript). Using JavaScript, you can create interactive web pages along with desktop widgets, browser, and application extensions, and other pieces of software. Object-oriented programming, which is popularly known as OOP, is basically based on the concept of objects rather than actions. The first module will help you master JavaScript and build futuristic web applications. You will start by getting acquainted with the language constructs and how to organize code easily. You develop concrete understanding of variable scoping, loops, and best practices on using types and data structures, as well as the coding style and recommended code organization patterns in JavaScript. The book will also teach you how to use arrays and objects as data structures. By the end of the book, you will understand how reactive JavaScript is going to be the new paradigm. The second module is an easy-to-follow course, which includes hands-on examples of solutions to common problems with object-oriented code. It will help to identify objects from real-life scenarios, to protect and hide data with the data encapsulation features of Python, JavaScript, and C#. You will discover the advantage of duck typing in both Python and JavaScript, while you work with interfaces and generics in C#. With a fair understanding of interfaces, multiple inheritance, and composition, you will move on to refactor existing code and to organize your source for easy maintenance and extension. The third module takes you through all the in-depth and exciting futures hidden behind the facade. You should read through this course if you want to be able to take your JavaScript skills to a new level of sophistication. Style and approach This course is a comprehensive guide where each chapter consists of best practices, constructive advice, and few easy-to-follow examples that will build up your skills as you advance through the book. Get object oriented with this course, which takes you on a journey to get acquainted with few useful hands-on tools, features, and ways to enhance your productivity using OOP techniques. It will also act as a reference guide with useful examples on resolving problems with object-oriented code in Python, JavaScript, and C#. Table of Contents Chapter 1: Javascript Primer Chapter 2: Functions, Closures, And Modules Chapter 3: Data Structures And Manipulation Chapter 4: Object-Oriented Javascript Chapter 5: Javascript Patterns Chapter 6: Testing And Debugging Chapter 7: Ecmascript 6 Chapter 8: Dom Manipulation And Events Chapter 9: Server-Side Javascript Chapter 10: Objects Everywhere Chapter 11: Classes And Instances Chapter 12: Encapsulation Of Data Chapter 13: Inheritance And Specialization Chapter 14: Interfaces, Multiple Inheritance, And Composition Chapter 15: Duck Typing And Generics Chapter 16: Organization Of Object-Oriented Code Chapter 17: Taking Full Advantage Of Object-Oriented Programming Chapter 18: Object-Oriented Javascript Chapter 19: Primitive Data Types, Arrays, Loops, And Conditions Chapter 20: Functions Chapter 21: Objects Chapter 22: Prototype Chapter 23: Inheritance Chapter 24: The Browser Environment Chapter 25: Coding And Design Patterns Chapter 26: Reserved Words Chapter 27: Built-In Functions Chapter 28: Built-In Objects Chapter 29: Regular Expressions
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

youyouxiong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值