角落

记录一下生活和思考

javascript之创建对象

学过面向对象编程的同学一定都知道,类是对象的模板,对象是根据模板创建的。

可是Javascript中没有类的概念,只有对象的概念,没有类怎么创建对象呢?

下面介绍两种创建javascript对象方法:

  • 直接赋值法
  • 构造函数法

赋值法

1
2
3
4
5
6
7
8
9
10
11
12
13
var shopProduct = {
title:'上衣', // 对象的属性
"brand":"优衣库",
'price':300, //key可以加引号或者双引号。
getSunmary:function(){ //对象的方法
return "titile: "+this.title+", brand: "+this.brand+", price: "+this.price;
}
}

// 输出对象属性
console.log(shopProduct.title);
console.log(shopProduct.price);
console.log(shopProduct.brand);

构造函数法

先定义一个函数,然后用关键字new调用它,就会得到一个对象。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function  ShopProduct(name,brand,price)
{
this.name =name;
this.brand= brand;
this.price = price;
this.getSunmary = function(){
return "titile: "+this.name+", brand: "+this.brand+", price: "+this.price;
};

return this;

}

shoes = new ShopProduct('shoes','nike',500);

console.log(shoes.name);
console.log(shoes.price);
console.log(shoes.brand);
console.log(shoes.getSunmary());

注意:

  1. 构造函数中的this指向新对象。
  2. 构造函数中虽然没有return,但是默认返回this。

库、工具包、框架、设计模式、架构、编程范式

清晰正确的概念,有助于我们认识世界,甚至可以被当做工具用来改造世界。
对于库、工具包、框架、设计模式、架构、编程范式这些概念的正确理解,同样有助我们认识虚拟的软件工程的世界。
对这些概念的相关知识的掌握,同样可以用来建设和改造软件。

下面是我对库、工具包、框架、设计模式、架构、编程范式这些概念的理解。

库和工具包:

库和工具包侧重于代码重用。
从微观上解决具体问题,相当于士兵的武器装备。
比如javascript的jQuery库。

框架:

框架侧重于设计重用。
从宏观上控制软件整体的结构和流程,规范程序员的编码。
比如Java的Spring框架。

设计模式:

设计模式侧重于思想重用,针对某些经常出现的问题而提出的行之有效的设计解决方案。
设计模式有几十种,比如单例模式,工程模式、适配器模式等等。

架构:

架构一般指一个软件系统的最高层次的整体结构和规划。
架构涉及具体的软件产品,不同类型的软件产品,因为业务的不同,架构也不一样, 比如微信的技术架构和微博的技术架构就不一样。
一般一个架构可能包含多个框架,而一个框架可能包含多个设计模式。

编程范式:

编程范式是计算机编程中的基本风格和典范,是代码中所蕴含的世界观和方法论,
每种范式都引导人们使用其特有的倾向和思路去分析和解决问题。
比如命令式编程,函数式编程,面向对象编程等等。

Javascript之JSON

什么是JSON

JSON的全称是Javascript Object Notation, 是一种轻量级数据交换格式。

JSON的数据类型

实际上JSON就是Javascript的一个子集,所以它的数据类型和Javascript基本一样,类型也比较少.

  • number = javascript的number
  • boolean = javascript的boolean
  • string = javascript的string
  • null = javascript的null
  • array = javascript的array
  • object = javascript的object

作为一种数据交换格式,为了能统一解析,规定了字符串和对象的键值必须用双引号“”。

如何在Javascript中使用JSON?

序列化

把数据放入一个Javascript对象,再把该对象序列化成一个JSON格式的字符串——序列化,然后通过网络传递到其他系统。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var jim = {
name: 'Jim',
'age':21,
skills:['html','css','php','javascript'],
}

j = JSON.stringify(jim,null,' ');
console.log(j);
// {
// "name": "Jim",
// "age": 21,
// "skills": [
// "html",
// "css",
// "php",
// "javascript"
// ]
// }

反序列化

当收到的JSON格式的字符串时,把字符串反序列化成一个JSON对象,就可以在Javascript中直接使用了

1
2
3
4
5
6
7
8
9
rj = '{"name": "Jim","age": 21,"skills": ["html","css","php","javascript"]}';

obj = JSON.parse(rj);

console.log(obj);

// { name: 'Jim',
// age: 21,
// skills: [ 'html', 'css', 'php', 'javascript' ] }

javascript闭包

什么是闭包?

高阶函数除了可以接受一个或多个函数作为参数,还可以返回一个函数作为结果。

当一个函数和它的返回函数满足下面情况,就是闭包。

  1. 函数A的返回值是函数B;
  2. 当函数A返回函数B时, 函数B引用了函数A内的变量;
  3. 函数B并不是马上执行,而是在调用B()后执行

示例

在购物时,所有商品的价格加起来的总价超过目标金额是,给予警告。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function warnAmount(amount) {  //函数A
count = 0;
return function(price) { //函数B 匿名函数, 函数B引用函数A的count变量和amount变量
count += price;
console.log('count: '+count);
if(count>amount){
console.log('High price reached:'+count);
}
}
}

add = warnAmount(10); //调用函数A,返回函数B,并将函数B赋值给变量add,

// 通过B()调用函数B,add()
add(1); // count: 1
add(2); // count: 3
add(3); // count: 6
add(4); // count: 10
add(5); // count: 15 High price reached:15

注意

在五次调用函数B-add(price)过程中,变量count和变量amount一直保存着他们的状态,
而在函数A之外,是不可以访问变量count和变量amount,
就像_面向对象编程中的私有变量。

从这个角度可以看出来,闭包就是携带状态的函数,并且它的状态可以对外隐藏起来。

所以我们也可以利用闭包在没有类特性的javascript中实现面向对象中编程中对象的私有变量。

Javascript箭头函数

什么是箭头函数

ES6新增一种函数:箭头函数,箭头函数相当于匿名函数

1
x=>x+y

等同于

1
2
3
function(x) {
return x+y;
}

当有多个参数,多条语句时

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var f = (x,y)=>{

if(x>y) {
return 1;
}

if(x<y){
return -1;
}

return 0;
}

console.log(f(1,3));

Javascript高阶函数

什么是高阶函数?

接受另一个函数作为参数的函数,就叫做高阶函数

一个简单的高阶函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

function compare(a,b,fn)
{
return fn(a,b);
}

var fn = function (a,b) {
if(a>b){
console.log('a>b');
} else if (a==b){
console.log('a=b');
} else {
console.log('a<b');
}
}

a =3 ;
b = 2;

compare(a,b,fn);

javacritp中的高阶函数

map

给数组每个一个字符串元素加一个前缀

1
2
3
4
5
6
7
8
var fn = function(s) {
return 'prefix_'+s;
}

var arr =['a','b','c','d'];
var result = arr.map(fn);

console.log(result);

reduce

求数组中所有数值元素的乘积

1
2
3
4
5
6
7
8
var arr = [1,2,3,4,5];

var result = arr.reduce(function(x,y) {
return x*y;
}
);

console.log(result);

filter

过滤数组中的空字符串

1
2
3
4
5
6
7
8
9
10
11
12
function removeEmpty(arr)
{
return arr.filter(function(x){
return x != '';
})
}

var arr = ['',1,2,3,''];

var result = removeEmpty(arr);

console.log(result);

sort

sort默认是按ASCII码排序,对数值也先转成字符串,再按ASCII排序。
所以直接使用sort排序,可能得到的意外的结果,但是可以传入一个函数作为参数自定义排序算法。

  1. 使数组中的数值按降序排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
arr.sort(function(x,y)
{
if(x<y) {
return 1;
}

if(x>y) {
return -1;
}

return 0;

}
);
  1. 对数组中的字符串忽略大小写排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

var arr = ['Google','apple','Microsoft']

arr.sort(function (x,y)
{
x1 = x.toUpperCase();
y1 = y.toUpperCase();

if(x1<y1) {
return -1;
}
if(x1>y1){
return 1;
}
return 0;
}
);

console.log(arr);

我是从四月份开始读纳尼亚传奇,具体开始时间没有记录,还用每次读完一本,在微信朋友圈有发记录,下面是读一本书所用天数统计。 每天大概2个多小时。

书名 开始时间 结束时间 天数
The Lion,The Witch and The Wardrobe 未知 4月20日 未知
Prince Caspian 4月21日 5月18日 28天
The Voyage of The Dawn Treader 5月19日 6月2日 15天
The Silver Chair 6月3日 7月3日 30天
The Horse and His Boy 7月4日 7月11日 8天
The Magician’s Nephew 7月20日 7月27日 8天
Last Battle 7月31日 8月6日 7天

花钱是为了更好的挣钱

1.周围那些“拎不清”的人们

找朋友代购和在国内买便宜不了多少。

2.生活的本质是交换

时间成本
信用成本
心情成本

哪一个成本最重要?
时间成本。

3.租房为什么要离公司近

更多的时间,可以用来提升自己。

4.花钱是为了更好的挣钱

用钱换时间

花在学习上的钱不能少, 比如培训,买书。

开始写文章

  1. 逻辑清晰
  2. 每天坚持写,就有进步
  3. 加入共同成长,开始语言学习的分享
  4. 加入新生大学创作大学。

5.真正的困境,不知道失去什么?

因为从来没有得到拥有习得一种能力的好处,所以从来不觉得生活中失去了什么。

6.钱和梦想其实有相同的内核

钱和梦想之间选择什么?

赚钱和实现梦想都需要勤奋、坚持、上进。

另外实现梦想还需要钱。

形容词

形容词的作用是修饰名词。

广义的形容词包括

  1. 形容词从句
  2. 简化形容词从句(分词短语、同位语、不定词)
  3. 介词
  4. 复合词及单词等等。

主要讲解单词形状的形容词, 他们在句子中只有两个位置:

  • 名词短语中
  • 补语位置

名词短语中的形容词

一般出现在限定词和名词中间,用来表示该名词属性,叫做attributive adjectives。

限定词 形容词 名词
three yellow roses
a new camera
my best friend
dirty water
pretty women

一、放在名词后面的形容词

Someone else will have to do it.

I don’t know anybody else.

解释:因为限定词some和any已经和名词one、body合并在一起了,没有中间位置,只能把else放到名词后面去了。

John and his brother alike are unreliable.

Money alone cannot solve our problem.

解释: alike和alone都是a开头的单词,在古英语中表示一种暂时性的状态,不适合放在名词短语中间表示名词属性的位置。

二、名词专用为形容词

限定词 形容词 名词
a government store
my pencil sharpener
a cigarette box
movie theaters

解释:虽然他们都是名词,一旦放入形容词位置,就转为形容词使用,且同形容词一样没有复数。

三、复合词形容词

a turn-of-the-century publication

an eye-opening experience

a five-year-old child

a 100-watt light bulb

解释:如果是短语要放入名词短语的中间,必须加上“-”组成复合词,并把短语中的复数名词改成单数,因为形容词单词,不能有复数。

名词短语中形容词的顺序

在名词短语中,出现两个以上的形容,就会产生顺序的问题。

顺序的规则:越是表达名词属性的形容词越要靠近名词。 即:

  • 越是不可变得、客观的特质越要靠近名词。
  • 越是可变的、临时的、主观的因素越要放得远离名词。

The murderer left behind a bloody old black Italian leather glove.

He’s wearing a handsome old brown U.S Air Force leather flying jacket.

形容词在名词短语位置与补语位置的比较

名词短语中的形容词叫做:attributive adjectives。

补语位置的形容词叫:predicative adjectives。

补语形容词离名词最远,用来补述名词,对名词作一些临时性、补充性的叙述。

  1. John is sick today and couldn’t come to work. (predicative)

    短暂性,可能过了今天就好了。

  2. John is a sick man. (attributive)

    永久性,短时间好不了。

补语位置的形容词

这个位置的形容词比较自由,单词、短语都可以。

This lake is deep.
S C

She makes everyone happy.
O C

Chinese culture is 5000 years old.
S C

I heard her playing the violin.
O C

a开头的古英语形容词,不适合放在名词短语中间位置,但是适合出现在补语位置。表示“暂时性”语气。

The fish is still alive.
S C

They found the professor alone.
O C

形容词的比较级

形容词比较级,有三种逻辑关系:1. 大于 2. 小于 3. 等于。

Unit 3 is shorter than Unit 4.

Unit 3 is less difficult than Unit 4.

Unit 3 is as boring as Unit 4.

一、比较级的拼法

单音节形容词,因为很短,所以适合在词尾变化.

tall, taller, tallest

三个音节以上的形容词,因为太长,所以不适合词尾变化。

expensive, more expensive, most expensive

两个音节的形容词,如果词尾有典型的形容词词尾标示词类,应保留词尾不变,分成两个词处理,其他双音节单词随意。

典型双音节形容词词尾,加more和most
例如:crowded, loving, helpful, useless, famous, active.

其他双音节形容词

often,shallow

二、定冠词的判断

错误原则:最高级加定冠词

正确原则:冠词是跟名词走的。

  1. 在名词短语中的形容词,加冠词。
  2. 补语位置,且不在名词短语中,不加定冠词。

YueXiuShan is the most crowded of Guangzhou’s scenic spots.
the most crowed one fo Guangzhou’s scenic spots.

YueXiuShan is most crowded in March.

John is the shorter of the twins.

三、that和those的使用

比较级的句子要求对称工整,必须要包含比较对象在句子里。

My car is bigger than yours (not you).

Cars made in China are better than those (not it/they) made in Korea.

如果用they,就是先行词,代表cars made in China,只能用those,代表those cars made in Korea。

四、比较级的倒装

A chimp has as much I.Q as a child of five or six does.
S V

A chimp has as much I.Q as does a child of five or sic.

解释:

does取代has I.Q避免重复;

但是does远离它所代表的部分,和它的主语a child也有距离,这些距离有碍句子的清楚流程,所以倒装能避免这些问题。

结语

形容词比较级比较容易出问题,尤其对称性要求与省略变化。

读《文法俱乐部》分析长句—从句简化成分词结构

今天早上刚学习完《文法俱乐部》分词部分

其中有提到形容词从句简化成分词结构和副词从句简化成分词结构。

在Yahoo Digest今天报道的“Nearly 120 killed in overnight Baghdad bombings claimed by ISIS 在巴干达将近120人被ISIS自杀式炸弹炸死”里看到这两种简化.

学新东西,一碰到用的机会,就要马上用起来,先来分析一下这个句子的结构。

新闻原句

A refrigerator truck packed with explosives blew up
in the central district of Karrada,
killing 115 people and injuring at least 200.

还原

A refrigerator which was packed with explosives blew up in the central district of Karrada, when it was killing 115 people and injuring at least 200.

简化过程

形容词从句简化:

A refrigerator which was packed with explosives blew up in the central district of Karrada,

which和A regrigerator重复,去掉;

be动词was无实际意思,去掉。

剩下过去分词结构packed with explosives作为形容词类。

过去分词可看做形容词,所以在句子中无词类冲突

副词从句简化:

A refrigerator packed with explosives blew up
in the central district of Karrada,
when it was killing 115 people and injuring at least 200.

it和A refrigerator重复,去掉;

be动词was,无实际意思,去掉;

when连接词,表示和blew up同时发生,killing已经有暗示这个意思,重复,去掉。

剩下现在分词killing 115 people和injuring at least 200作为形容词类。

现在分词可看做形容词,所以在句子中无词类冲突。

0%