加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 建站 > 正文

跨过编程入门门槛,从写一首“代码诗”开始

发布时间:2019-07-02 20:49:55 所属栏目:建站 来源:读芯术
导读:对很多人来说,学写代码很难。本文将通过编写与众不同且富有诗意的代码来克服学习代码最初遇到的困难。如果你是个JavaScript新手,或者是在编程学习过程中遇到了困难,本文介绍的方法也许可以帮到你。 为什么学习编程这么难? 以下是人们对编程很难学及Java

但在JavaScript中,如果不定义“love”,就没有与之对应的表达,至于形式,则完全取决于你。

  1. var love = { 
  2.  color: ‘red’, 
  3.  duration: 365, 
  4.  loveTarget: ‘cats’, 
  5. }; 
  6. // a simple variable expression, 
  7. // where love is an object “{ … }”, a thing 
  8. // with some properties (color, duration, loveTarget). 
  9. const love2 = { 
  10.  color: ‘purple’, 
  11.  duration: ‘forever’, 
  12.  loveTarget: ‘dogs’, 
  13. }; 
  14. // also a variable expression, where love2 (aconstant), 
  15. // cannot be redefined / overwritten completely: 
  16. // love2 = undefined; // => will not work 
  17. // (“undefined” is a pre-defined javascriptkeyword, 
  18. // basically saying “has no value”) 

区分JavaScript中预定义的内容(JavaScript规则和词汇表)与开发人员实际自定义的内容(也称为“应用程序逻辑”或“业务逻辑”)十分重要。

回到上面写的诗:

  1. // Love at first sight 
  2. if (me.getDistanceTo(you.position) < 200) { 
  3.  me.setFeelings({ 
  4.  inLove: true, 
  5.  }); 

这些表达式来自以下JavaScript词汇表规则集:

  1. if (…) { … } 
  2. // if statement: when … is met, do things in { … } 
  3.  inLove: true, 
  4. // an “object” with some info, some thing in the world. 
  5. // can contain other info, and “skills” (functions). 
  6. // “inLove” is a custom property, 
  7. // “true” is pre-defined in javascript, (meaning: “yes”) 
  8. // and the value of “inLove”. 
  9. // needed to access an objects property “my name: me.name” 
  10. getDistanceTo() 
  11. // an expression to “call” a function (a “skill”). 
  12. // getDistanceTo is custom (not JavaScript), and a function, 
  13. // so it can be executed / called upon with the “()” after. 
  14. // sometimes you can pass arguments in those brackets (like “position”) 
  15. // to change the outcome of a function. 

这些是变量,可以自定义它们的名称和行为。

  1. me // an object, some thing in the world 
  2. you // an object, some thing in the world 
  3. position // an info about “you”, accessed by the “.” 
  4. getDistanceTo // a skill of me, accessed by the “.” 
  5. getDistanceTo() // the skill, with javascript grammar telling: do it. 
  6. getDistanceTo(position) // same, but do it with “position”. 
  7. setFeelings // another skill of me, accessed by the “.” 
  8. setFeelings({ inLove: true }); // the skill, with some instructions (anobject). 

假设这是一首人类读得懂的诗,你可能已经理解了其中的信息,也可能看到了需要遵循的JavaScript语言规则与需要提出的内容(变量)之间有何区别。

但机器又会怎么做呢?

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读