# object.assign基本用法
# Object.assign()方法
## 对象的扩展
1.ES6中,对象的属性和方法可简写:对象的属性值可不写,前提是属性名已经声明;
```
<pre class="calibre13">```
<span class="token2">var</span> name <span class="token">=</span> <span class="token4">"zhangsan"</span><span class="token1">;</span> <span class="token2">var</span> password <span class="token">=</span> <span class="token4">"1111111"</span><span class="token1">;</span> <span class="token2">var</span> obj <span class="token">=</span> <span class="token1">{</span> name<span class="token1">,</span> password<span class="token1">,</span> arr<span class="token1">:</span><span class="token1">[</span><span class="token5">1</span><span class="token1">,</span><span class="token5">2</span><span class="token1">,</span><span class="token5">3</span><span class="token1">,</span><span class="token5">4</span><span class="token1">]</span><span class="token1">,</span> <span class="token3">sayName</span><span class="token1">(</span><span class="token1">)</span><span class="token1">{</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span><span class="token2">this</span><span class="token1">.</span>name<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span><span class="token1">;</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>obj<span class="token1">)</span><span class="token1">;</span> <span class="token2">var</span> obj1 <span class="token">=</span> <span class="token1">{</span> age<span class="token1">:</span><span class="token5">20</span><span class="token1">,</span> <span class="token1">}</span><span class="token1">;</span>
```
```
### 2.Object.assign()方法
特点:浅拷贝、对象属性的合并
```
<pre class="calibre13">```
<span class="token2">var</span> nObj <span class="token">=</span> Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span><span class="token1">{</span><span class="token1">}</span><span class="token1">,</span>obj<span class="token1">,</span>obj1<span class="token1">)</span><span class="token1">;</span><span class="token6">//花括号叫目标对象,后面的obj、obj1是源对象。对象合并是指:将源对象里面的属性添加到目标对象中去,若两者的属性名有冲突,后面的将会覆盖前面的 // console.log(nObj); // obj.sayName();//zhangsan // nObj.sayName();//zhangsan nObj.name = "lisi"; nObj.sayName = function(){ console.log(this.name); }; console.log(nObj); obj.sayName();//zhangsan nObj.sayName();//lisi nObj.arr[0] = 10; console.log(obj);//[10,2,3,4] console.log(nObj); //[10,2,3,4] //说明是浅拷贝,若要深拷贝,请考虑以前的方法(只是将源对象的引用给目标对象) //【注意】:当Object.assign()方法用于数组时: var arr11 = Object.assign([1,2,3],[4,5]); console.log(arr11);//[4,5,3] //[说明]:对象是根据属性名来对应,数组是根据索引号来对应,相当于 var arr23 = { 0:1, 1:2, 2:3 };//相同的属性名有0、1,后面的覆盖前面的.</span>
```
```
### Object.assign()方法的用法
1. 合并多个对象
```
<pre class="calibre14">```
<span class="token2">const</span> obj1 <span class="token">=</span> <span class="token1">{</span> name<span class="token1">:</span> <span class="token4">'张三'</span><span class="token1">,</span>age<span class="token1">:</span> <span class="token5">20</span><span class="token1">}</span><span class="token2">const</span> obj2 <span class="token">=</span> <span class="token1">{</span> address<span class="token1">:</span> <span class="token4">'广东深圳'</span><span class="token1">,</span> hobby<span class="token1">:</span> <span class="token4">'code'</span> <span class="token1">}</span><span class="token2">const</span> obj3 <span class="token">=</span> <span class="token1">{</span> workingYears<span class="token1">:</span> <span class="token5">5</span><span class="token1">}</span> <span class="token2">const</span> obj <span class="token">=</span> Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span>obj1<span class="token1">,</span> obj2<span class="token1">,</span> obj3<span class="token1">)</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>obj<span class="token1">)</span> <span class="token6">//{ name: '张三', age: 20, address: '广东深圳', hobby: 'code', workingYears: 5}</span>
```
```
2. 克隆对象(浅);
```
<pre class="calibre14">```
<span class="token2">const</span> obj <span class="token">=</span> <span class="token1">{</span> name<span class="token1">:</span> <span class="token4">'张三'</span> <span class="token1">,</span>age<span class="token1">:</span> <span class="token5">20</span><span class="token1">}</span> <span class="token2">const</span> obj1 <span class="token">=</span> Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span><span class="token1">{</span><span class="token1">}</span><span class="token1">,</span>obj<span class="token1">)</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>obj1<span class="token1">)</span> <span class="token6">//{ name: '张三' ,age: 20}</span>
```
```
3. 为对象添加多个方法
```
<pre class="calibre14">```
Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span>SomeClass<span class="token1">.</span>prototype<span class="token1">,</span> <span class="token1">{</span> <span class="token3">someMethod</span><span class="token1">(</span>arg1<span class="token1">,</span> arg2<span class="token1">)</span> <span class="token1">{</span> <span class="token1">}</span><span class="token1">,</span> <span class="token3">anotherMethod</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token1">}</span><span class="token1">}</span><span class="token1">)</span><span class="token1">;</span><span class="token6">// 原来的方法SomeClass.prototype.someMethod = function (arg1, arg2) {};SomeClass.prototype.anotherMethod = function () {};</span>
```
```
# 【ES6学习笔记之】Object.assign()
## 基本用法
`Object.assign`方法用于对象的合并,将源对象(source)的所有可枚举属性,复制到目标对象(target)。
```
<pre class="calibre13">```
const target <span class="token">=</span> <span class="token1">{</span> a<span class="token1">:</span> <span class="token5">1</span> <span class="token1">}</span><span class="token1">;</span>
const source1 <span class="token">=</span> <span class="token1">{</span> b<span class="token1">:</span> <span class="token5">2</span> <span class="token1">}</span><span class="token1">;</span>
const source2 <span class="token">=</span> <span class="token1">{</span> c<span class="token1">:</span> <span class="token5">3</span> <span class="token1">}</span><span class="token1">;</span>
Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span>target<span class="token1">,</span> source1<span class="token1">,</span> source2<span class="token1">)</span><span class="token1">;</span>
target <span class="token6">// {a:1, b:2, c:3}</span>
```
```
`Object.assign`方法的第一个参数是目标对象,后面的参数都是源对象。
注意,如果目标对象与源对象有同名属性,或多个源对象有同名属性,则后面的属性会覆盖前面的属性。
```
<pre class="calibre13">```
const target <span class="token">=</span> <span class="token1">{</span> a<span class="token1">:</span> <span class="token5">1</span><span class="token1">,</span> b<span class="token1">:</span> <span class="token5">1</span> <span class="token1">}</span><span class="token1">;</span>
const source1 <span class="token">=</span> <span class="token1">{</span> b<span class="token1">:</span> <span class="token5">2</span><span class="token1">,</span> c<span class="token1">:</span> <span class="token5">2</span> <span class="token1">}</span><span class="token1">;</span>
const source2 <span class="token">=</span> <span class="token1">{</span> c<span class="token1">:</span> <span class="token5">3</span> <span class="token1">}</span><span class="token1">;</span>
Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span>target<span class="token1">,</span> source1<span class="token1">,</span> source2<span class="token1">)</span><span class="token1">;</span>
target <span class="token6">// {a:1, b:2, c:3}</span>
```
```
如果只有一个参数,`Object.assign`会直接返回该参数。
```
<pre class="calibre13">```
const obj <span class="token">=</span> <span class="token1">{</span>a<span class="token1">:</span> <span class="token5">1</span><span class="token1">}</span><span class="token1">;</span>
Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span>obj<span class="token1">)</span> <span class="token">===</span> obj <span class="token6">// true</span>
```
```
如果该参数不是对象,则会先转成对象,然后返回。
```
<pre class="calibre13">```
typeof Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span><span class="token5">2</span><span class="token1">)</span> <span class="token6">// "object"</span>
```
```
由于`undefined`和`null`无法转成对象,所以如果它们作为参数,就会报错。
```
<pre class="calibre13">```
Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span>undefined<span class="token1">)</span> <span class="token6">// 报错</span>
Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span><span class="token2">null</span><span class="token1">)</span> <span class="token6">// 报错</span>
```
```
如果非对象参数出现在源对象的位置(即非首参数),那么处理规则有所不同。首先,这些参数都会转成对象,如果无法转成对象,就会跳过。这意味着,如果`undefined`和`null`不在首参数,就不会报错。
```
<pre class="calibre13">```
let obj <span class="token">=</span> <span class="token1">{</span>a<span class="token1">:</span> <span class="token5">1</span><span class="token1">}</span><span class="token1">;</span>
Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span>obj<span class="token1">,</span> undefined<span class="token1">)</span> <span class="token">===</span> obj <span class="token6">// true</span>
Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span>obj<span class="token1">,</span> <span class="token2">null</span><span class="token1">)</span> <span class="token">===</span> obj <span class="token6">// true</span>
```
```
其他类型的值(即数值、字符串和布尔值)不在首参数,也不会报错。但是,除了字符串会以数组形式,拷贝入目标对象,其他值都不会产生效果。
```
<pre class="calibre13">```
const v1 <span class="token">=</span> <span class="token4">'abc'</span><span class="token1">;</span>
const v2 <span class="token">=</span> <span class="token5">true</span><span class="token1">;</span>
const v3 <span class="token">=</span> <span class="token5">10</span><span class="token1">;</span>
const obj <span class="token">=</span> Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span><span class="token1">{</span><span class="token1">}</span><span class="token1">,</span> v1<span class="token1">,</span> v2<span class="token1">,</span> v3<span class="token1">)</span><span class="token1">;</span>
console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>obj<span class="token1">)</span><span class="token1">;</span> <span class="token6">// { "0": "a", "1": "b", "2": "c" }</span>
```
```
上面代码中,`v1`、`v2`、`v3`分别是字符串、布尔值和数值,结果只有字符串合入目标对象(以字符数组的形式),数值和布尔值都会被忽略。这是因为只有字符串的包装对象,会产生可枚举属性。
```
<pre class="calibre13">```
<span class="token3">Object</span><span class="token1">(</span><span class="token5">true</span><span class="token1">)</span> <span class="token6">// {[[PrimitiveValue]]: true}</span>
<span class="token3">Object</span><span class="token1">(</span><span class="token5">10</span><span class="token1">)</span> <span class="token6">// {[[PrimitiveValue]]: 10}</span>
<span class="token3">Object</span><span class="token1">(</span><span class="token4">'abc'</span><span class="token1">)</span> <span class="token6">// {0: "a", 1: "b", 2: "c", length: 3, [[PrimitiveValue]]: "abc"}</span>
```
```
上面代码中,布尔值、数值、字符串分别转成对应的包装对象,可以看到它们的原始值都在包装对象的内部属性`[[PrimitiveValue]]`上面,这个属性是不会被`Object.assign`拷贝的。只有字符串的包装对象,会产生可枚举的实义属性,那些属性则会被拷贝。
`Object.assign`拷贝的属性是有限制的,只拷贝源对象的自身属性(不拷贝继承属性),也不拷贝不可枚举的属性(`enumerable: false`)。
```
<pre class="calibre13">```
Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span><span class="token1">{</span>b<span class="token1">:</span> <span class="token4">'c'</span><span class="token1">}</span><span class="token1">,</span>
Object<span class="token1">.</span><span class="token3">defineProperty</span><span class="token1">(</span><span class="token1">{</span><span class="token1">}</span><span class="token1">,</span> <span class="token4">'invisible'</span><span class="token1">,</span> <span class="token1">{</span>
enumerable<span class="token1">:</span> <span class="token5">false</span><span class="token1">,</span>
value<span class="token1">:</span> <span class="token4">'hello'</span>
<span class="token1">}</span><span class="token1">)</span>
<span class="token1">)</span>
<span class="token6">// { b: 'c' }</span>
```
```
上面代码中,`Object.assign`要拷贝的对象只有一个不可枚举属性`invisible`,这个属性并没有被拷贝进去。
属性名为 Symbol 值的属性,也会被`Object.assign`拷贝。
```
<pre class="calibre13">```
Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span><span class="token1">{</span> a<span class="token1">:</span> <span class="token4">'b'</span> <span class="token1">}</span><span class="token1">,</span> <span class="token1">{</span> <span class="token1">[</span><span class="token3">Symbol</span><span class="token1">(</span><span class="token4">'c'</span><span class="token1">)</span><span class="token1">]</span><span class="token1">:</span> <span class="token4">'d'</span> <span class="token1">}</span><span class="token1">)</span>
<span class="token6">// { a: 'b', Symbol(c): 'd' }</span>
```
```
- - - - - -
## 注意点
### (1)浅拷贝
`Object.assign`方法实行的是浅拷贝,而不是深拷贝。也就是说,如果源对象某个属性的值是对象,那么目标对象拷贝得到的是这个对象的引用。
```
<pre class="calibre13">```
const obj1 <span class="token">=</span> <span class="token1">{</span>a<span class="token1">:</span> <span class="token1">{</span>b<span class="token1">:</span> <span class="token5">1</span><span class="token1">}</span><span class="token1">}</span><span class="token1">;</span>
const obj2 <span class="token">=</span> Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span><span class="token1">{</span><span class="token1">}</span><span class="token1">,</span> obj1<span class="token1">)</span><span class="token1">;</span>
obj1<span class="token1">.</span>a<span class="token1">.</span>b <span class="token">=</span> <span class="token5">2</span><span class="token1">;</span>
obj2<span class="token1">.</span>a<span class="token1">.</span>b <span class="token6">// 2</span>
```
```
上面代码中,源对象`obj1`的`a`属性的值是一个对象,`Object.assign`拷贝得到的是这个对象的引用。这个对象的任何变化,都会反映到目标对象上面。
### (2)同名属性的替换
对于这种嵌套的对象,一旦遇到同名属性,`Object.assign`的处理方法是替换,而不是添加。
```
<pre class="calibre13">```
const target <span class="token">=</span> <span class="token1">{</span> a<span class="token1">:</span> <span class="token1">{</span> b<span class="token1">:</span> <span class="token4">'c'</span><span class="token1">,</span> d<span class="token1">:</span> <span class="token4">'e'</span> <span class="token1">}</span> <span class="token1">}</span>
const source <span class="token">=</span> <span class="token1">{</span> a<span class="token1">:</span> <span class="token1">{</span> b<span class="token1">:</span> <span class="token4">'hello'</span> <span class="token1">}</span> <span class="token1">}</span>
Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span>target<span class="token1">,</span> source<span class="token1">)</span>
<span class="token6">// { a: { b: 'hello' } }</span>
```
```
上面代码中,`target`对象的`a`属性被`source`对象的`a`属性整个替换掉了,而不会得到`{ a: { b: 'hello', d: 'e' } }`的结果。这通常不是开发者想要的,需要特别小心。
一些函数库提供`Object.assign`的定制版本(比如 Lodash 的`_.defaultsDeep`方法),可以得到深拷贝的合并。
### (3)数组的处理
`Object.assign`可以用来处理数组,但是会把数组视为对象。
```
<pre class="calibre13">```
Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span><span class="token1">[</span><span class="token5">1</span><span class="token1">,</span> <span class="token5">2</span><span class="token1">,</span> <span class="token5">3</span><span class="token1">]</span><span class="token1">,</span> <span class="token1">[</span><span class="token5">4</span><span class="token1">,</span> <span class="token5">5</span><span class="token1">]</span><span class="token1">)</span>
<span class="token6">// [4, 5, 3]</span>
```
```
上面代码中,`Object.assign`把数组视为属性名为 0、1、2 的对象,因此源数组的 0 号属性`4`覆盖了目标数组的 0 号属性`1`。
### (4)取值函数的处理
`Object.assign`只能进行值的复制,如果要复制的值是一个取值函数,那么将求值后再复制。
```
<pre class="calibre13">```
const source <span class="token">=</span> <span class="token1">{</span>
get <span class="token3">foo</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token5">1</span> <span class="token1">}</span>
<span class="token1">}</span><span class="token1">;</span>
const target <span class="token">=</span> <span class="token1">{</span><span class="token1">}</span><span class="token1">;</span>
Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span>target<span class="token1">,</span> source<span class="token1">)</span>
<span class="token6">// { foo: 1 }</span>
```
```
上面代码中,`source`对象的`foo`属性是一个取值函数,`Object.assign`不会复制这个取值函数,只会拿到值以后,将这个值复制过去。
## 常见用途
`Object.assign`方法有很多用处。
### (1)为对象添加属性
```
<pre class="calibre13">```
class <span class="token3">Point</span> <span class="token1">{</span>
<span class="token3">constructor</span><span class="token1">(</span>x<span class="token1">,</span> y<span class="token1">)</span> <span class="token1">{</span>
Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span>this<span class="token1">,</span> <span class="token1">{</span>x<span class="token1">,</span> y<span class="token1">}</span><span class="token1">)</span><span class="token1">;</span>
<span class="token1">}</span>
<span class="token1">}</span>
```
```
上面方法通过`Object.assign`方法,将`x`属性和`y`属性添加到`Point`类的对象实例。
### (2)为对象添加方法
```
<pre class="calibre13">```
Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span>SomeClass<span class="token1">.</span>prototype<span class="token1">,</span> <span class="token1">{</span>
<span class="token3">someMethod</span><span class="token1">(</span>arg1<span class="token1">,</span> arg2<span class="token1">)</span> <span class="token1">{</span>
···
<span class="token1">}</span><span class="token1">,</span>
<span class="token3">anotherMethod</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span>
···
<span class="token1">}</span>
<span class="token1">}</span><span class="token1">)</span><span class="token1">;</span>
<span class="token6">// 等同于下面的写法</span>
SomeClass<span class="token1">.</span>prototype<span class="token1">.</span>someMethod <span class="token">=</span> <span class="token2">function</span> <span class="token1">(</span>arg1<span class="token1">,</span> arg2<span class="token1">)</span> <span class="token1">{</span>
···
<span class="token1">}</span><span class="token1">;</span>
SomeClass<span class="token1">.</span>prototype<span class="token1">.</span>anotherMethod <span class="token">=</span> <span class="token2">function</span> <span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span>
···
<span class="token1">}</span><span class="token1">;</span>
```
```
上面代码使用了对象属性的简洁表示法,直接将两个函数放在大括号中,再使用`assign`方法添加到`SomeClass.prototype`之中。
### (3)克隆对象
```
<pre class="calibre13">```
<span class="token2">function</span> <span class="token3">clone</span><span class="token1">(</span>origin<span class="token1">)</span> <span class="token1">{</span>
<span class="token2">return</span> Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span><span class="token1">{</span><span class="token1">}</span><span class="token1">,</span> origin<span class="token1">)</span><span class="token1">;</span>
<span class="token1">}</span>
```
```
上面代码将原始对象拷贝到一个空对象,就得到了原始对象的克隆。
不过,采用这种方法克隆,只能克隆原始对象自身的值,不能克隆它继承的值。如果想要保持继承链,可以采用下面的代码。
```
<pre class="calibre13">```
<span class="token2">function</span> <span class="token3">clone</span><span class="token1">(</span>origin<span class="token1">)</span> <span class="token1">{</span>
let originProto <span class="token">=</span> Object<span class="token1">.</span><span class="token3">getPrototypeOf</span><span class="token1">(</span>origin<span class="token1">)</span><span class="token1">;</span>
<span class="token2">return</span> Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span>Object<span class="token1">.</span><span class="token3">create</span><span class="token1">(</span>originProto<span class="token1">)</span><span class="token1">,</span> origin<span class="token1">)</span><span class="token1">;</span>
<span class="token1">}</span>
```
```
### (4)合并多个对象
将多个对象合并到某个对象。
```
<pre class="calibre13">```
const merge <span class="token">=</span> <span class="token1">(</span>target<span class="token1">,</span> <span class="token1">.</span><span class="token1">.</span><span class="token1">.</span>sources<span class="token1">)</span> <span class="token">=</span><span class="token">></span> Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span>target<span class="token1">,</span> <span class="token1">.</span><span class="token1">.</span><span class="token1">.</span>sources<span class="token1">)</span><span class="token1">;</span>
```
```
如果希望合并后返回一个新对象,可以改写上面函数,对一个空对象合并。
```
<pre class="calibre13">```
const merge <span class="token">=</span> <span class="token1">(</span><span class="token1">.</span><span class="token1">.</span><span class="token1">.</span>sources<span class="token1">)</span> <span class="token">=</span><span class="token">></span> Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span><span class="token1">{</span><span class="token1">}</span><span class="token1">,</span> <span class="token1">.</span><span class="token1">.</span><span class="token1">.</span>sources<span class="token1">)</span><span class="token1">;</span>
```
```
### (5)为属性指定默认值
```
<pre class="calibre13">```
const DEFAULTS <span class="token">=</span> <span class="token1">{</span>
logLevel<span class="token1">:</span> <span class="token5">0</span><span class="token1">,</span>
outputFormat<span class="token1">:</span> <span class="token4">'html'</span>
<span class="token1">}</span><span class="token1">;</span>
<span class="token2">function</span> <span class="token3">processContent</span><span class="token1">(</span>options<span class="token1">)</span> <span class="token1">{</span>
options <span class="token">=</span> Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span><span class="token1">{</span><span class="token1">}</span><span class="token1">,</span> DEFAULTS<span class="token1">,</span> options<span class="token1">)</span><span class="token1">;</span>
console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>options<span class="token1">)</span><span class="token1">;</span>
<span class="token6">// ...</span>
<span class="token1">}</span>
```
```
上面代码中,`DEFAULTS`对象是默认值,`options`对象是用户提供的参数。`Object.assign`方法将`DEFAULTS`和`options`合并成一个新对象,如果两者有同名属性,则`option`的属性值会覆盖`DEFAULTS`的属性值。
注意,由于存在浅拷贝的问题,`DEFAULTS`对象和`options`对象的所有属性的值,最好都是简单类型,不要指向另一个对象。否则,`DEFAULTS`对象的该属性很可能不起作用。
```
<pre class="calibre13">```
const DEFAULTS <span class="token">=</span> <span class="token1">{</span>
url<span class="token1">:</span> <span class="token1">{</span>
host<span class="token1">:</span> <span class="token4">'example.com'</span><span class="token1">,</span>
port<span class="token1">:</span> <span class="token5">7070</span>
<span class="token1">}</span><span class="token1">,</span>
<span class="token1">}</span><span class="token1">;</span>
<span class="token3">processContent</span><span class="token1">(</span><span class="token1">{</span> url<span class="token1">:</span> <span class="token1">{</span>port<span class="token1">:</span> <span class="token5">8000</span><span class="token1">}</span> <span class="token1">}</span><span class="token1">)</span>
<span class="token6">// {</span>
<span class="token6">// url: {port: 8000}</span>
<span class="token6">// }</span>
```
```
上面代码的原意是将`url.port`改成 8000,`url.host`不变。实际结果却是`options.url`覆盖掉`DEFAULTS.url`,所以`url.host`就不存在了。
- fastadmin教程
- fastadmin原始分类的操作方法
- fastadmin关联官方和自方法修改
- fastadmin下拉搜索异步
- fastadmin 语法解释
- fastadmin 一对多和in的用法以及搜索方法
- fasttadmin表格
- xdebug的安装和使用在phpstorm
- thinkphp教程
- thinkphp 一对多 多对一的源码实例
- thinkphp跨域的解决办法
- thinkphp使用redis
- thinkphp常用判断语句
- thinkphp的显示更新 自动判断是否需要更新
- thinkphp5一对多或者多对一
- thinkphp5方法注入
- thinkphp map回调函数用法
- thinkphp6.0数据库上
- thinkphp6.0数据库下
- thinkphp6.0模型关联
- 命令行操作MYSQL
- thinkphp6.0依赖注入 容器 门面
- thinkphp6 伪静态参数绑定开启缓存
- Mysql 索引教程
- mysql 索引其他知识
- desc(降序)asc(升序) inc(自增) dec(自减)
- 什么是ORM?为什么要用ORM?
- thinkphp操作mysql部分
- thinkphp的自动加载
- thinkphp5.1中的配置文件
- thinkphp5.1容器和门面模式
- tp5 单例模式
- 注册树模式
- 依赖注入
- 反射机制(补充部分内容)
- Countable的使用
- 简单Contaniner容器
- thinkphp5.1容器流程分析
- thinkphp6中间件
- thinkphp6 Facade门面
- thinkphp中间件
- 导航菜单递归
- 图片转base64
- php教程
- php序列化和反序列化解析
- php封装POST或者GET请求
- 字符串转换成数组
- 帝国CMS常用函数
- php操作Memcached对象
- php操作fetch
- 帝国cms所有数据库字段说明
- [优化sql提高查询速度]
- 深入分析PHP优化及注意事项
- MySQL性能优化的最佳21条经验
- MySQL全文索引Match Against与Like比较
- mysql常用记录
- php curl
- php截取字符串
- php字符串处理
- php 13位时间戳
- php操作数据库
- php对接码支付
- php常用概念
- php 构造函数和析构函数的区别
- PHP的join的用法
- mysql 索引教程
- php 数组处理
- php 闭包传值
- RBAC权限管理
- php -- 取路径:getcwd()、DIR、FILE 的区别
- php字符串下标
- jwt
- uni-app教程
- uniapp页面跳转
- uniaapp文件配置目录
- uniapp引入ui组件
- uniapp引入全局变量
- uniapp页面传参
- uniapp从接口获取数据
- uniapp制作验证码倒计时定时器
- uniapp 同步上传图片
- uniapp时间戳改成正常时间
- uniapp目录的作用
- 小数四舍五入缩短小数位数
- 自定义tarbar
- uniapp页面通讯
- uniapp之this作用域
- 关闭侧滑
- 开发概念讲解
- 跨域概念
- 微信QQ等手机UA
- autojs悬浮窗以及url访问
- python3 使用flak开发网站
- python3怎么用sqlalchemy操作mysql
- python3使用alchemy操作mysql完成代码
- python使用alchemy处理一对一和反向引用
- python3 sqlalchemy 一对一关系
- flask中使用sqlalchemy处理多对多
- flask_script插件的使用
- javascript教程
- jq制作定时器
- 右侧悬浮弹窗可关闭
- jq ajax请求
- prototype
- toLowerCase()基本使用
- Array数组的join()方法
- javascript回调函数详解
- jquery中$.ajax()方法使用详解
- js中的回调函数
- js中every和some的区别
- js实现本地搜索
- html本地传输数据
- 前端CSS教程
- css rem单位 em单位 和定位
- 自适应
- 前端定位方式
- flex布局下img图片变形的解决方法
- css样式代码段
- css居中
- position和display和float区别
- css工具或者导航
- border边框
- 正则表达式
- 匹配键盘可见特殊符号
- 微信小程序教程
- 微信小程序整理
- 小程序开发框架
- 小程序支付
- 微信小程序自定义组件
- 微信小程序配置文件介绍
- 模块化
- 视图层
- centos linux 图文教程
- centos easyswoole
- es6教程
- ES6 let 与 const
- ES6 解构赋值
- ES6 Symbol
- ES6 Map 与 Set
- ES6 Reflect 与 Proxy
- ES6 字符串
- ES6中循环
- ES6函数的扩展
- ES6模块
- ES6 class类
- promise 对象
- async和await
- ES6对象属性简写
- object.assign基本用法
- object.keys()基本用法
- es6展开运算符
- es6的导入导出方式
- 织梦cms
- 不同的分类显示不同的图片
- 正则表达式php教程
- git基本使用教程
- git 清空缓存
- nodeJS
- nodejs 会话技术
- 网络爬虫
- nodejs操作数据库
- nodejs中resolve()在url中的使用方法
- nvm node版本管理工具
- nrm
- npm安装的区别
- Buffer类
- Vue教程
- VUE安装
- vue脚手架
- vue创建路由
- Vuex
- vue懒加载
- axios网络请求
- ES6 Promise 对象
- ES6 async 函数
- vue绑定元素
- vue回调函数的理解
- indexof()
- vue生命周期
- router-view和keep-alive
- Mustache插值操作
- 计算属性 computed
- Locker实现localStrage的简便方法
- this.$router.push replace go的用法
- vue中使用refs 完成密码框明密文切换
- 计算属性和method的对比
- 父子组件之间的通信
- vue中路由的基本使用
- $router和$route的区别
- prototype 原型的使用
- vue的导航守卫
- vue的生命周期
- vue 路由hash改成history
- vue中路由详解
- vue中的回调函数
- slot插槽
- vuecli3 跨域
- vue和thinkphp中的前端设置代理跨域
- js中push pop unshift shift的区别
- 计算属性computed如何传参
- vue或uniapp中this.$set()时的语法格式
- 计算属性 vs 侦听属性 watch
- elementui片段
- vue修饰符
- vue---reduce高级与filter去重
- indexOf 和 includes
- require.context()的用法
- vite构建
- vue3.2新特性
- axios封装中间件和避免重复请求
- vue-router 4.x笔记
- Redis基本教程
- laravel基本教程
- 路由
- 视图
- 模型
- 创建数据表和操作数据库
- windows服务器清理mysql残留
- mysql语句
- mysql 语句整理一
- mysql概念和性能优化
- mysql常用工作遇到
- mysql
- mysql 5.7 groupby
- implode()和explode()
- mysql语句部分
- Mysql使用函数json_extract处理Json类型数据
- linux 基础教程
- linux常用命令
- linux 安装redis
- linux 安装nodejs
- linux软连接的创建 删除和修改
- pm2使用方法
- linux 编译和安装软件包
- linux操作用户
- centos8安装mysql5.7
- vscode操作eslint用法
- Docker教程
- JAVA
- implements Serializable的作用
- mac系统操作教程
- mac安装redis扩展
- 升级mac自带的php版本
- brew
- 占用端口
- GO笔记整理
- 目录结构
- 下划线
- 变量
- 切片
- 指针
- map
- 匿名字段
- nil含义
- socket
- 并发
- goroutine调度
- 管道
- GO正则表达式
- iota
- go moudles
- go 中函数的对比
- golang 参数传值
- Casbin
- 基本类型
- 实例化结构体
- 原生sql设置变量
- ------代码片段-------
- uniapp
- uniapp样式
- uniapp上传
- vue封装html转成excel
- yyladmin
- array_merge
- vuex部分
- vue前端部分
- yyladmin前端安装流程
- windows安装linux子系统
- yum和apt的区别