未加星标

Javascript: My Next Language

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

I started learning javascript a couple weeks ago, in short early morning sprints before breakfast, 4 or 5 times a week. My goal is to learn vue.js, as a way to create Progressive Web Apps (PWAs).

I knew some things about Javascript when I started learning Javascript. Javascript treats functions as first class objects, which means that Javascript allows functions to be stored in variables, passed as parameters or returned, or modified on the fly, just like objects or arrays.

Object Oriented Programming (OOP) focuses on functions and objects. When I saw a Udemy course “Object Oriented Programming in Javascript”, I decided that would be a great place to start learning Javascript.

Let’s start with what bits I learned, following the pattern inYour Next Language.

Obvious Unexpected discovery

"use strict";

Similar to Perl’s use strict; , this line puts your ECMAScript 2015 compliant Javascript engine into a “sane” version of Javascript that is much safer to write code in. Mozilla has details .

Put this line at the top of files of new code in Javascript, or at the top of new functions in existing Javascript files.

Don’t put it at the top of an existing Javascript file, unless you have really great tests, or lots of time to fix all the errors, and to manually check all the customer visible interactions.

Blocks

Surrounded by braces: {code block}

Loops and Decisions:

Loop over a list:

for (let variable in listname) { } Functions

(at least?) two ways to declare functions:

function FuncName(optionalParam1, optionalParam2) { // do stuff } let foo = function() { // do stuff }

Comments: line and block

// one line comment console.log('hello'); // inline at end of line of code /* block comments */ Variables

Old var keyword - don’t use:

var myVar; // do not use var!!

New keywords: let, const:

let myVar; const myConstant; // the const is unchangeable, but not things inside it

Variable types:

The following is working code.

"use strict"; let myNum = 9; // number const bookmark = 'Page 9'; // string - defined as a constant let isDoorClosed = true; // boolean let authors = ['Heinlein', 'Shakespear', 'Rowling']; // array let allNight = { // object upLate: true, topicStudied: 'Belinda' } console.log('myNum ' + myNum); console.log('bookmark ' + bookmark); console.log('isDoorClosed ' + isDoorClosed); console.log('Authors:\n' + authors); console.log('\nobject: '); for (let item in allNight) { console.log(item, allNight[item]); }

Consider using object syntax where you would use a hash. Some hash methods won’t be available with this simple technique. There are libraries that provide full HashTable functionality.

This sample also demonstrates a for loop.

Passing variables by value, by reference

Value types, also called primitive types:

Symbol, Number, Boolean, String, undefined, null.

Reference types:

Functions, Objects, Arrays.

When you copy a primitive, you copy the value.

When you copy an object, you copy the reference to it.

When you call a function with an item as a parameter, the item is copied.

If it is a primitive, you are looking at a copy in the function.

If it is an object, you are looking at a copy of the reference in the function, but the object referred to is the same object, so when you change the object, you are changing the original.

Using the public package repository, and packagemanager

I can only say generalizations here, so far.

Node.js has a package manager, npm, for use in your development environment, and on servers. There are others.

Using packages in the browser means collecting them with npm, or other package manager.

Reducing page startup time is apparently a big problem, so there are tools to compress packages for download to the browser, and even break them up so code is only downloaded when you click on the button that needs it.

Breaking long lines into readablepieces

I haven’t investigated that yet.

Finally

This has, for me, been a huge post that took a lot of time in my vacation. I will post more about my current knowledge in Javascript soon.

As usual, writing about this material means I have learned it more thoroughly, and I have had fun doing this!

Let me know in the comments where I have got it wrong. And try out what you see!

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

tags: Javascript,log,object,use
分页:12
转载请注明
本文标题:Javascript: My Next Language
本站链接:https://www.codesec.net/view/586425.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(96)