js获取对象属性方式

Vue取参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const path = require('path') //引入path文件
function resolve(dir) {
// console.log('引入vue.config.js')
return path.join(__dirname, dir) //path.join(__dirname)设置绝对路径
}

module.exports = {
productionSourceMap:false,
assetsDir: 'static',
publicPath: './',
chainWebpack: (config) => {
console.log('vue.config.js.chainWebpack')
config.resolve.alias
.set('@', resolve('src'))
.set('components', resolve('src/components'))
.set('views', resolve('src/views'))
.set('assets', resolve('src/assets'))
.set('network', resolve('src/network'))
.set('utils', resolve('src/utils'))
//set第一个参数:设置的别名,第二个参数:设置的路径
},
devServer: {
proxy: {
'/api': {
// target: "http://47.106.188.9:8081",
target: "http://127.0.0.1:8081",
// target: 'http://192.168.1.171:8081',
// target: 'http://192.168.1.16:8081',
ws: true,
changeOrigin: true,
pathRewrite: {
// '^/api': '/api',
'^/api': ''
}
},
// 仿写,当遇到 api_wx路由将url替换为新的地址
'/api_wx': {
target: "http://127.0.0.1:8081",
ws: true,
changeOrigin: true,
pathRewrite: {
'^/api_wx': ''
}
}
}
}
}
1
2
3
4
5
6
7
8
// 导入配置文件
import dev from "../../../vue.config.js"

exportDevicesTable(){
exportDevices({ids:this.multipleSelection}).then(res=>{
window.location.href = dev.devServer.proxy['/api'].target + res;
})
}

js中对象获取属性的两种表示方法

使用 .

点 一般操作静态对象来存取属性

1
2
3
4
5
6
7
mounted(){
let obj = {
a:"m1",
b:"m2"
}
console.log("a---",obj.a)//m1
}

我们平常开发中会多使用第一种方式,系统会把调用的obj.a自动隐式转换为: obj[‘a’]来执行,所以第二种会第一种快捷。

使用 [ ] (又称数组表示法)

中括号一般操作动态对象来存取属性

1
2
3
4
5
6
7
mounted(){
let obj = {
a:"m1",
b:"m2"
}
console.log("b---",obj['b'])//m2
}

共同点:以上两种都可以实现属性的存取。
区别:
第一种obj.a 点后面是对象中的属性名,必须是一个指控的属性名。第二种 obj[‘a’] []里面必须是字符型。

使用场景:
当动态为某个对象添加属性时用第二种合适。动态操作对象时。

1
2
3
4
5
6
7
8
9
10
11
12
mounted(){
let obj1 = {
haha1:{a1:1,b1:1},
haha2:{a2:2,b2:2},
haha3:{a3:3,b3:3},
btn:function(num){
return obj1['haha'+num]
}
}
let result = obj1.btn(3)
console.log("result---",result) // {a3: 3, b3: 3}
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!