但在JavaScript中,如果不定义“love”,就没有与之对应的表达,至于形式,则完全取决于你。
- var love = {
- color: ‘red’,
- duration: 365,
- loveTarget: ‘cats’,
- };
- // a simple variable expression,
- // where love is an object “{ … }”, a thing
- // with some properties (color, duration, loveTarget).
- const love2 = {
- color: ‘purple’,
- duration: ‘forever’,
- loveTarget: ‘dogs’,
- };
- // also a variable expression, where love2 (aconstant),
- // cannot be redefined / overwritten completely:
- // love2 = undefined; // => will not work
- // (“undefined” is a pre-defined javascriptkeyword,
- // basically saying “has no value”)
区分JavaScript中预定义的内容(JavaScript规则和词汇表)与开发人员实际自定义的内容(也称为“应用程序逻辑”或“业务逻辑”)十分重要。
回到上面写的诗:
- // Love at first sight
- if (me.getDistanceTo(you.position) < 200) {
- me.setFeelings({
- inLove: true,
- });
- }
这些表达式来自以下JavaScript词汇表规则集:
- if (…) { … }
- // if statement: when … is met, do things in { … }
- {
- inLove: true,
- }
- // an “object” with some info, some thing in the world.
- // can contain other info, and “skills” (functions).
- // “inLove” is a custom property,
- // “true” is pre-defined in javascript, (meaning: “yes”)
- // and the value of “inLove”.
- .
- // needed to access an objects property “my name: me.name”
- getDistanceTo()
- // an expression to “call” a function (a “skill”).
- // getDistanceTo is custom (not JavaScript), and a function,
- // so it can be executed / called upon with the “()” after.
- // sometimes you can pass arguments in those brackets (like “position”)
- // to change the outcome of a function.
这些是变量,可以自定义它们的名称和行为。
- me // an object, some thing in the world
- you // an object, some thing in the world
- position // an info about “you”, accessed by the “.”
- getDistanceTo // a skill of me, accessed by the “.”
- getDistanceTo() // the skill, with javascript grammar telling: do it.
- getDistanceTo(position) // same, but do it with “position”.
- setFeelings // another skill of me, accessed by the “.”
- setFeelings({ inLove: true }); // the skill, with some instructions (anobject).
假设这是一首人类读得懂的诗,你可能已经理解了其中的信息,也可能看到了需要遵循的JavaScript语言规则与需要提出的内容(变量)之间有何区别。
但机器又会怎么做呢?
(编辑:ASP站长网)
|