由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请问个javascript object 怎么访问属性的问题
相关主题
Functional programming 是大势所趋周末上点有用的信息
今天跟公司confirm了,没有人用Scala学了这么多语言发现还是coffeescript最好用
scala/clojure/groovy/Jython/jruby都是噱头CoffeeScript: The beautiful way to write JavaScript
Python和perl都属于那种很难精通的语言为什么说Javascript 是披着C 外衣的Lisp?
2013年tiobe的变化JS就应该杜绝OOP
coffeescript令人着魔javascript最让人崩溃的一点
我钟爱的coffeescript这次牛大了 (转载)TypeScript看起来不错额:为Javascript增加静态类型, 类, 继承等
目前看就是做web,python也被落在最后CoffeeScript, TypeScript 能否在将来顶替 JavaScript?
相关话题的讨论汇总
话题: promise话题: body话题: email话题: do话题: undefined
进入Programming版参与讨论
1 (共1页)
f*****w
发帖数: 2602
1
入门问题,肯定是我什么地方理解错了 但是不明白为啥
这样的两行代码
console.log('req.body:' + JSON.stringify(req.body))
console.log('email:' + req.body['email'])
输出是:
req.body:{"body":{"email":"[email protected]
/* */"}}
email:undefined
我不明白为什么 ,明明我在body的内容里面是有email这个属性的。而且我尝试了其他
的reference的方法,都是说undefined
请问这是为啥啊?
p*****2
发帖数: 21240
2
看起来body有两层呀

/* */"}}

【在 f*****w 的大作中提到】
: 入门问题,肯定是我什么地方理解错了 但是不明白为啥
: 这样的两行代码
: console.log('req.body:' + JSON.stringify(req.body))
: console.log('email:' + req.body['email'])
: 输出是:
: req.body:{"body":{"email":"[email protected]
: /* */"}}
: email:undefined
: 我不明白为什么 ,明明我在body的内容里面是有email这个属性的。而且我尝试了其他
: 的reference的方法,都是说undefined

f*****w
发帖数: 2602
3
body之下还有两层? 我看上去只有一层啊
难道不是req.body.email就到leaf了么

【在 p*****2 的大作中提到】
: 看起来body有两层呀
:
: /* */"}}

f*****w
发帖数: 2602
4
ah ... 明白了 stupid

【在 p*****2 的大作中提到】
: 看起来body有两层呀
:
: /* */"}}

g*********e
发帖数: 14401
5
{email:"dsf@dfrd"}这个object没法直接print吧?
n*****t
发帖数: 22014
6
body.body.email

【在 f*****w 的大作中提到】
: body之下还有两层? 我看上去只有一层啊
: 难道不是req.body.email就到leaf了么

l**********n
发帖数: 8443
7
这不明显吗?这也来问。

/* */"}}

【在 f*****w 的大作中提到】
: 入门问题,肯定是我什么地方理解错了 但是不明白为啥
: 这样的两行代码
: console.log('req.body:' + JSON.stringify(req.body))
: console.log('email:' + req.body['email'])
: 输出是:
: req.body:{"body":{"email":"[email protected]
: /* */"}}
: email:undefined
: 我不明白为什么 ,明明我在body的内容里面是有email这个属性的。而且我尝试了其他
: 的reference的方法,都是说undefined

p*****2
发帖数: 21240
8
人家说了是入门呀

【在 l**********n 的大作中提到】
: 这不明显吗?这也来问。
:
: /* */"}}

f*****w
发帖数: 2602
9
不好意思啊 浪费大家时间了
新手。。。以为自己访问方法不对, body里面有body因为名字完全一样还以为输出的
是上一层的变量
及其蠢的问题。。。
非常感谢大家回复

【在 l**********n 的大作中提到】
: 这不明显吗?这也来问。
:
: /* */"}}

p*****2
发帖数: 21240
10
这种问题常见 不算什么
老手有时候也会犯类似问题

【在 f*****w 的大作中提到】
: 不好意思啊 浪费大家时间了
: 新手。。。以为自己访问方法不对, body里面有body因为名字完全一样还以为输出的
: 是上一层的变量
: 及其蠢的问题。。。
: 非常感谢大家回复

相关主题
coffeescript令人着魔周末上点有用的信息
我钟爱的coffeescript这次牛大了 (转载)学了这么多语言发现还是coffeescript最好用
目前看就是做web,python也被落在最后CoffeeScript: The beautiful way to write JavaScript
进入Programming版参与讨论
W***o
发帖数: 6519
11
to get email, it should be req.body.body.email
ps: does it wear a ma-jia ?

/* */"}}

【在 f*****w 的大作中提到】
: 入门问题,肯定是我什么地方理解错了 但是不明白为啥
: 这样的两行代码
: console.log('req.body:' + JSON.stringify(req.body))
: console.log('email:' + req.body['email'])
: 输出是:
: req.body:{"body":{"email":"[email protected]
: /* */"}}
: email:undefined
: 我不明白为什么 ,明明我在body的内容里面是有email这个属性的。而且我尝试了其他
: 的reference的方法,都是说undefined

l**********n
发帖数: 8443
12
这种情况我会加一层,把要的剥离出来,以表明这是返回的格式脑残,总之我不会写
req.body.body, 这样写法不好,万一返回的格式变了,又要重写。

【在 f*****w 的大作中提到】
: 不好意思啊 浪费大家时间了
: 新手。。。以为自己访问方法不对, body里面有body因为名字完全一样还以为输出的
: 是上一层的变量
: 及其蠢的问题。。。
: 非常感谢大家回复

l**********n
发帖数: 8443
13
如果是scala, 这种情况就不会出现。

【在 l**********n 的大作中提到】
: 这种情况我会加一层,把要的剥离出来,以表明这是返回的格式脑残,总之我不会写
: req.body.body, 这样写法不好,万一返回的格式变了,又要重写。

f*****w
发帖数: 2602
14
没很明白 能展开说说吗

【在 l**********n 的大作中提到】
: 这种情况我会加一层,把要的剥离出来,以表明这是返回的格式脑残,总之我不会写
: req.body.body, 这样写法不好,万一返回的格式变了,又要重写。

l**********n
发帖数: 8443
15
functional programming, 就是data transformation, functions are very easy to
test.
the idea is before you process the response received from the server side.
do some pre-processing on them, like sanity check, even you can throw Error
if the response is not what you expect.
The idea is that you just pass a Promise around.
A Promise has three state: unresolved, resolved, and rejected.
There are many ways to create a Promise, using a constructor,
new Promise(function(resolve, reject){
// do your processing here, once processing done
// you can do
resolve(value)
// or
reject(reason)
})
You can directly do:
Promise.resolve(value)
or Promise.reject(reason)
If something is a thenable, you can do:
Promise.resolve(thenable)
finally you can do
promise.then(function(value){
})
.catch(function(err){
})
you can even catch specific errors with the help of bluebird.
Because I know the response has been sanity checked, I can safely use it.
Also Promise holds the exception too so I can do something else if something
went wrong.

【在 f*****w 的大作中提到】
: 没很明白 能展开说说吗
p*****2
发帖数: 21240
16

to
Error
req?.body?.body

【在 l**********n 的大作中提到】
: functional programming, 就是data transformation, functions are very easy to
: test.
: the idea is before you process the response received from the server side.
: do some pre-processing on them, like sanity check, even you can throw Error
: if the response is not what you expect.
: The idea is that you just pass a Promise around.
: A Promise has three state: unresolved, resolved, and rejected.
: There are many ways to create a Promise, using a constructor,
: new Promise(function(resolve, reject){
: // do your processing here, once processing done

l**********n
发帖数: 8443
17
how you process exception in your way?
Scala has Promise too.

【在 p*****2 的大作中提到】
:
: to
: Error
: req?.body?.body

L***s
发帖数: 1148
18

那个是coffeescript写法,跟exception没关系
x = req?.body?.body 编译成js就是一坨
x = typeof req !== 'undefined' && req !== null ?
( (ref = req.body) != null ? ref.body : void 0 ) : void 0;

【在 l**********n 的大作中提到】
: how you process exception in your way?
: Scala has Promise too.

p*****2
发帖数: 21240
19

还是大牛聪明

【在 L***s 的大作中提到】
:
: 那个是coffeescript写法,跟exception没关系
: x = req?.body?.body 编译成js就是一坨
: x = typeof req !== 'undefined' && req !== null ?
: ( (ref = req.body) != null ? ref.body : void 0 ) : void 0;

L***s
发帖数: 1148
20

我只是帮二爷做做注脚

【在 p*****2 的大作中提到】
:
: 还是大牛聪明

1 (共1页)
进入Programming版参与讨论
相关主题
CoffeeScript, TypeScript 能否在将来顶替 JavaScript?2013年tiobe的变化
You Don’t Know JS: this & Object Prototypescoffeescript令人着魔
October Headline: Dart enters the top 20 for the first time我钟爱的coffeescript这次牛大了 (转载)
October Headline: Dart enters the top 20 for the first time (转载)目前看就是做web,python也被落在最后
Functional programming 是大势所趋周末上点有用的信息
今天跟公司confirm了,没有人用Scala学了这么多语言发现还是coffeescript最好用
scala/clojure/groovy/Jython/jruby都是噱头CoffeeScript: The beautiful way to write JavaScript
Python和perl都属于那种很难精通的语言为什么说Javascript 是披着C 外衣的Lisp?
相关话题的讨论汇总
话题: promise话题: body话题: email话题: do话题: undefined