1. console.log(3 / 2)
    2. console.log(Math.floor(3 / 2))
    3. console.log(0.1 + 0.2)
    4. console.log(Math.sign(3.5))
    5. console.log(2 * Math.PI * 10)
    6. // 输出63
    7. console.log(parseInt('0x3F'));
    8. console.log(parseInt('0x3F', 16));
    9. console.log(parseInt("063"))
    10. console.log(parseInt("63", 10))
    11. console.log(parseInt('77', 8));
    12. console.log(parseInt('111111', 2));
    13. console.log(+ "0x3f")
    14. console.log(NaN + 10)
    15. console.log(isNaN(NaN))
    16. console.log(isNaN(parseInt("2", 2)))
    17. console.log(1 / 0)
    18. console.log(-1 / 0)
    19. console.log(isFinite(Infinity))
    20. console.log(isFinite(NaN))
    21. console.log(isFinite(-Infinity))
    22. console.log(undefined)
    23. /// 字符串
    24. var str = "Hello Word"
    25. console.log(str.length)
    26. console.log(str.split(" "))
    27. console.log(str.charAt(1))
    28. console.log(str.replace("Word", "JsonYe"))
    29. console.log(str.toLowerCase())
    30. console.log(str.toLocaleLowerCase())
    31. console.log(str.toUpperCase())
    32. console.log(str.toLocaleUpperCase())
    33. /// Bool
    34. console.log(Boolean(''))
    35. console.log(Boolean('1'))
    36. console.log(Boolean(null))
    37. console.log(Boolean(NaN))
    38. console.log(Boolean(undefined))
    39. console.log(Boolean(0))
    40. console.log(Boolean(false))
    41. console.log(Boolean(Infinity))
    42. console.log(Boolean(-Infinity))
    43. /// 变量(let,const,var)
    44. // let 声明一个块级作用域的本地变量,并且可选的将其初始化为一个值。
    45. let age = 12
    46. // console.log(index) // 报错
    47. for (let index = 0; index < 2; index++) {
    48. // index 只能在此代码块中被引用
    49. console.log(index)
    50. }
    51. // console.log(index) // 报错
    52. // const 允许声明一个不可变的常量。这个常量在定义域内总是可见的,声明时需要初始化。
    53. const PI = 3.1415926
    54. console.log(PI)
    55. console.log(myVarVariable) // 可以使用
    56. for (var myVarVariable = 0; myVarVariable < 2; myVarVariable++) {
    57. console.log(myVarVariable) // 可以使用
    58. }
    59. console.log(myVarVariable) // 可以使用
    60. /// 运算符
    61. var x = 3
    62. console.log(x)
    63. x += 4
    64. console.log(x)
    65. console.log("hello" + " " + "word")
    66. console.log("3" + 4 + 5)
    67. console.log(3 + "4" + 5)
    68. console.log(3 + 4 + "5")
    69. console.log(123 == "123")
    70. console.log(1 == true)
    71. console.log(0 == false)
    72. /// 控制语句
    73. var name = "JsonYe"
    74. if (name == "JsonYe") {
    75. name += " !"
    76. } else {
    77. name = "! " + name
    78. }
    79. console.log(name)
    80. var flag = 0
    81. var sum = 0
    82. while (flag <= 10) {
    83. sum += flag
    84. flag++
    85. }
    86. console.log(sum)
    87. sum = 0
    88. flag = 0
    89. do {
    90. sum += flag
    91. flag++
    92. } while (flag <= 10)
    93. console.log(sum)
    94. sum = 0
    95. flag = 0
    96. for (; flag <= 10; flag++) {
    97. sum += flag
    98. }
    99. console.log(sum)
    100. var uName = "Json"
    101. uName = NaN
    102. var nName = uName || "json"
    103. console.log(nName)
    104. var obj = new Object()
    105. console.log(obj)
    106. var obj2 = {
    107. name: "JsonYe",
    108. age: 18,
    109. book: {
    110. name: "Android",
    111. price: 12
    112. }
    113. }
    114. console.log(obj2["name"])
    115. console.log(obj2.name)
    116. console.log(obj2["book"]["name"])
    117. console.log(obj2.book.name)
    118. function Person(name, age, book) {
    119. this.name = name;
    120. this.age = age;
    121. this.book = book;
    122. }
    123. function Book(name, price) {
    124. this.name = name;
    125. this.price = price;
    126. }
    127. var jsonYe = new Person("JsonYe", 18, new Book("Android", 12));
    128. console.log(jsonYe.name)
    129. console.log(jsonYe["name"])
    130. console.log(jsonYe.book.name)
    131. console.log(jsonYe["book"]["name"])
    132. console.log(jsonYe.book.price)
    133. console.log(jsonYe["book"]["price"])
    134. /// 数组
    135. var array = new Array()
    136. array.push("A")
    137. array[2] = "B"
    138. array[100] = 100
    139. array.push("C")
    140. array.push(jsonYe)
    141. console.log(array.length)
    142. console.log(array.join(","))
    143. console.log(typeof (array[99]))
    144. console.log(typeof (array[100]))
    145. console.log(typeof (array[101]))
    146. console.log(typeof (array[102]))
    147. function add() {
    148. var sum = 0;
    149. for (var i = 0, j = arguments.length; i < j; i++) {
    150. sum += arguments[i];
    151. }
    152. return sum;
    153. }
    154. // function avg() {
    155. // var sum = 0;
    156. // for (let index = 0; index < arguments.length; index++) {
    157. // sum += arguments[index]
    158. // }
    159. // return sum / arguments.length;
    160. // }
    161. function avg(...args) {
    162. var sum = 0;
    163. for (let index = 0; index < args.length; index++) {
    164. sum += args[index]
    165. }
    166. return sum / args.length;
    167. }
    168. console.log(add(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
    169. console.log(avg(1, 2, 4))
    170. console.log(avg.apply(null, [1, 2, 3]))
    171. /// 匿名
    172. var avg2 = function () {
    173. var sum = 0;
    174. for (let index = 0; index < arguments.length; index++) {
    175. sum += arguments[index];
    176. }
    177. return sum / arguments.length;
    178. }
    179. console.log(avg2(1, 2, 3, 4, 5, 6))
    180. function t() {
    181. var a = 1;
    182. var b = 2;
    183. // 定义了一个匿名行数
    184. (function () {
    185. var b = 3;
    186. a += b;
    187. }());// 执行匿名函数
    188. console.log("a=" + a + ",b=" + b)
    189. }
    190. t()
    191. function fb(n) {
    192. if (n == 1) return 0;
    193. if (n == 2) return 1;
    194. return fb(n - 1) + fb(n - 2);
    195. }
    196. console.log(fb(40))
    197. /// 闭包
    198. function makeAdd(a) {
    199. return function (b) {
    200. return a + b;
    201. }
    202. }
    203. var add5 = makeAdd(5)
    204. var add20 = makeAdd(20)
    205. console.log(add5(6))
    206. console.log(add20(6))
    207. class Polygon {
    208. constructor(name) {
    209. this._name = name;
    210. }
    211. get name() {
    212. return this._name;
    213. }
    214. set name(value) {
    215. this._name = value;
    216. }
    217. talk() {
    218. console.log(this._name);
    219. }
    220. static PI = 3.1415
    221. static sex() {
    222. console.log("男")
    223. }
    224. }
    225. const poly1 = new Polygon("JsonYe");
    226. poly1.talk()
    227. poly1.name = "LiuLi";
    228. poly1.talk()
    229. // console.log(poly1.PI)
    230. // poly1.sex()
    231. console.log(Polygon.PI)
    232. Polygon.sex()
    233. function PolygonTwo(name) {
    234. this.name = name;
    235. }
    236. PolygonTwo.prototype.speak = function () {
    237. return this.name;
    238. }
    239. PolygonTwo.prototype.add = function (a, b) {
    240. return a + b;
    241. }
    242. console.log(new PolygonTwo("JsonYe").speak())
    243. console.log(new PolygonTwo("").add(2, 5))
    244. class Rectangle {
    245. #num;
    246. constructor(height, width) {
    247. this.height = height;
    248. this.width = width;
    249. this.#num = 3;
    250. }
    251. speak() {
    252. return `${this.#num}-${this.height}`
    253. }
    254. }
    255. var rg = new Rectangle(12, 20);
    256. console.log("rg.height:" + rg.height)
    257. rg._height = 15;
    258. console.log("rg._height:" + rg._height);
    259. Rectangle.staticWidth = 20;
    260. console.log("Rectangle.staticWidth:" + Rectangle.staticWidth)
    261. console.log(rg.speak())
    262. /// 箭头函数
    263. const items = [
    264. "Goods", "Pad", "Apple", "Pick"
    265. ]
    266. console.log(items.map(item => item.length).join(","))