gzl的博客

  • 首页

  • 关于

  • 标签

  • 分类

  • 归档

LC-最大子序和

发表于 2019-08-11 更新于 2020-03-14 分类于 LeetCode

题目描述

最大子序和

给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。

1
2
3
输入: [-2,1,-3,4,-1,2,1,-5,4],
输出: 6
解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。

代码实现

下面这个解法的思想就是求各个位置的最大值

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function(nums) {
let prev = 0;
let theMax = -Number.MAX_VALUE;
for(let num of nums) {
prev = Math.max(prev + num, num);
theMax = Math.max(theMax, prev);
}
return theMax;
};

RN-《JavaScript高级程序设计》第六章创建对象

发表于 2019-08-07 更新于 2020-02-06 分类于 读书笔记

前言

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 字面量方式创建对象
let person1 = {
name: "abc",
age: 23,
sayHi: function () {
console.log(name);
}
};

// Object方式创建对象
let person2 = new Object();
person2.name = "def";
person2.age = 18;
person2.sayHi = function () {
console.log(person2.name);
}

虽然Object构造函数或对象字面量都可以用来创建单个对象,但这些方式有个明显的缺点:使用同一个接口创建很多对象,会产生大量的重复代码,如上面的代码,每创建一个类似的person对象,就会重复上面的写法,代码较为冗余。

工厂模式

1
2
3
4
5
6
7
8
9
10
11
12
function createPerson(name, age, job) {
let o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.sayName = function () {
alert(this.name);
};
return o;
}
let person1 = createPerson("Nicholas", 29, "Software Engineer");
let person2 = createPerson("Greg", 27, "Doctor");

工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题(即怎样知道一个对象的类型)。

构造函数模式

使用构造函数模式将前面的例子重写如下:

1
2
3
4
5
6
7
8
9
10
function Person(name, age, job) {
this.name = name;
this.age = age;
this.job = job;
this.sayName = function () {
alert(this.name);
};
}
let person1 = new Person("Nicholas", 29, "Software Engineer");
let person2 = new Person("Greg", 27, "Doctor");

要创建 Person 的新实例,必须使用 new 操作符。以这种方式调用构造函数实际上会经历以下 4个步骤:

  1. 创建一个新对象
  2. 将构造函数的作用域赋给新对象(因此 this 就指向了这个新对象)
  3. 执行构造函数中的代码(为这个新对象添加属性)
  4. 返回新对象(构造函数在不返回值的情况下,默认会返回新对象实例)

在前面例子的最后,person1 和 person2 分别保存着 Person 的一个不同的实例。这两个对象都有一个constructor(构造函数)属性,该属性指向 Person,如下所示。

1
2
3
4
5
6
7
console.log(person1.constructor == Person); //true
console.log(person2.constructor == Person); //true

console.log(person1 instanceof Object); //true
console.log(person1 instanceof Person); //true
console.log(person2 instanceof Object); //true
console.log(person2 instanceof Person); //true

创建自定义的构造函数意味着将来可以将它的实例标识为一种特定的类型;而这正是构造函数模式胜过工厂模式的地方。在这个例子中,person1 和 person2 之所以同时是 Object 的实例,是因为所有对象均继承自 Object。

使用构造函数的主要问题,就是每个方法(这里是sayname)都要在每个实例上重新创建一遍。

1
console.log(person1.sayName == person2.sayName) // false

原型模式

1
2
3
4
5
6
7
8
9
10
11
12
function Person() {}
Person.prototype.name = "Nicholas";
Person.prototype.age = 29;
Person.prototype.job = "Software Engineer";
Person.prototype.sayName = function () {
console.log(this.name);
};
let person1 = new Person();
person1.sayName(); //"Nicholas"
let person2 = new Person();
person2.sayName(); //"Nicholas"
console.log(person1.sayName == person2.sayName); //true

与构造函数模式不同的是,新对象的这些属性和方法是由所有实例共享的。换句话说,person1 和 person2 访问的都是同一组属性和同一个 sayName()函数。

更简单的原型语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function Person() {}
Person.prototype = {
name: "Nicholas",
age: 29,
job: "Software Engineer",
sayName: function () {
console.log(this.name);
}
};

let friend = new Person();
console.log(friend instanceof Object); //true
console.log(friend instanceof Person); //true
console.log(friend.constructor == Person); //false
console.log(friend.constructor == Object); //true

在上面的代码中,我们将 Person.prototype 设置为等于一个以对象字面量形式创建的新对象。最终结果相同,但有一个例外:constructor 属性不再指向 Person 了。前面曾经介绍过,每创建一个函数,就会同时创建它的prototype 对象,这个对象也会自动获得 constructor 属性。而我们在这里使用的语法,本质上完全重写了默认的 prototype 对象,因此 constructor 属性也就变成了新对象的 constructor 属性(指向 Object 构造函数),不再指向 Person 函数。此时,尽管 instanceof 操作符还能返回正确的结果,但通过 constructor 已经无法确定对象的类型了。

如果 constructor 的值真的很重要,可以像下面这样特意将它设置回适当的值。

1
2
3
4
5
6
7
8
9
10
function Person() {}
Person.prototype = {
constructor: Person,
name: "Nicholas",
age: 29,
job: "Software Engineer",
sayName: function () {
console.log(this.name);
}
};

调用构造函数时会为实例添加一个指向最初原型的 [[Prototype]] 指针,而把原型修改为另外一个对象就等于切断了构造函数与最初原型之间的联系。

请记住:实例中的指针仅指向原型,而不指向构造函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
function Person() {}
let friend = new Person();

Person.prototype = {
constructor: Person,
name: "Nicholas",
age: 29,
job: "Software Engineer",
sayName: function () {
console.log(this.name);
}
};
friend.sayName(); // Uncaught TypeError: friend.sayName is not a function

在上面的代码中,我们先创建了 Person 的一个实例,然后又重写了其原型对象。然后在调用 friend.sayName()时发生了错误,因为 friend 指向的原型中不包含以该名字命名的属性。

新地球

原型对象的问题

原型中所有属性是被很多实例共享的,这种共享对于函数非常合适。对于包含引用类型值的属性来说,问题就比较突出了。而这个问题正是我们很少看到有人单独使用原型模式的原因所在。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function Person() {}
Person.prototype = {
constructor: Person,
name: "Nicholas",
age: 29,
job: "Software Engineer",
friends: ["Shelby", "Court"],
sayName: function () {
console.log(this.name);
}
};
let person1 = new Person();
let person2 = new Person();
person1.friends.push("Van");
console.log(person1.friends); // ["Shelby", "Court", "Van"]
console.log(person2.friends); // ["Shelby", "Court", "Van"]
console.log(person1.friends === person2.friends); //true

组合使用构造函数模式和原型模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function Person(name, age, job) {
this.name = name;
this.age = age;
this.job = job;
this.friends = ["Shelby", "Court"];
}
Person.prototype = {
constructor: Person,
sayName: function () {
console.log(this.name);
}
}
let person1 = new Person("Nicholas", 29, "Software Engineer");
let person2 = new Person("Greg", 27, "Doctor");
person1.friends.push("Van");
console.log(person1.friends); // ["Shelby", "Court", "Van"]
console.log(person2.friends); // ["Shelby", "Court"]
console.log(person1.friends === person2.friends); //false
console.log(person1.sayName === person2.sayName); //true

在这个例子中,实例属性都是在构造函数中定义的,而由所有实例共享的属性 constructor 和方法 sayName()则是在原型中定义的。而修改了 person1.friends(向其中添加一个新字符串),并不会影响到 person2.friends,因为它们分别引用了不同的数组。

这种构造函数与原型混成的模式,是目前在 ECMAScript 中使用最广泛、认同度最高的一种创建自定义类型的方法。可以说,这是用来定义引用类型的一种默认模式。

JS类数组对象

发表于 2019-08-06 更新于 2019-08-12 分类于 JavaScript

类数组对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 属性要为索引 (数字) 属性,必须有length属性,最好加上push

Array.prototype.push = function (target) {
obj[obj.length] = target;
obj.length ++;
}

let obj = {
"0" : 'a',
"1" : 'b',
"2" : 'c',
"length" : 3,
"push" : Array.prototype.push,
"splice" : Array.prototype.splice
}
1
2
3
4
5
6
7
8
9
10
11
let obj = {			
"2" : 'a',
"3" : 'b',
"length" : 2,
"push" : Array.prototype.push
}
obj.push('c');
obj.push('d');

console.log(obj);
// {2: "c", 3: "d", length: 4, push: ƒ}

调用数组方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
let arrayLike = {
0: 'name',
1: 'age',
2: 'sex',
length: 3
}

Array.prototype.join.call(arrayLike, '&'); // name&age&sex

Array.prototype.slice.call(arrayLike, 0); // ["name", "age", "sex"]
// slice可以做到类数组转数组

Array.prototype.map.call(arrayLike, (item) => {
return item.toUpperCase();
});
// ["NAME", "AGE", "SEX"]

类数组转对象

1
2
3
4
5
6
7
8
9
10
11
let arrayLike = {
0: 'name',
1: 'age',
2: 'sex',
length: 3,
"push" : Array.prototype.push
}
// 1. slice
Array.prototype.slice.call(arrayLike); // ["name", "age", "sex"]
// 2. splice
Array.prototype.splice.call(arrayLike, 0); // ["name", "age", "sex"]

Use Array.from for converting an array-like object to an array.

用 Array.from 去将一个类数组对象转成一个数组。

1
2
3
4
5
6
7
const arrLike = { 0: 'foo', 1: 'bar', 2: 'baz', length: 3 };

// bad
const arr = Array.prototype.slice.call(arrLike);

// good
const arr = Array.from(arrLike);

arguments

说到类数组对象,arguments对象就是一个类数组对象。在客户端 JavaScript 中,一些 DOM 方法document.getElementsByTagName()等也返回类数组对象。

To convert an iterable object to an array, use spreads ... instead of Array.from.

用 ... 运算符而不是Array.from来将一个可迭代的对象转换成数组。

1
2
3
4
5
6
7
const foo = document.querySelectorAll('.foo');

// good
const nodes = Array.from(foo);

// best
const nodes = [...foo];

参考

JavaScript 深入之类数组对象与 arguments

https://github.com/airbnb/javascript

1…192021…32

gzl

96 日志
14 分类
37 标签
© 2020 gzl
由 Hexo 强力驱动 v3.7.1
|
主题 – NexT.Pisces v7.2.0