Let's see basic type and grammar of javascript. I assume that reader of this document have a basic programming experience. so, I will not explain everything in detail. We'll see javascript specific things and something is good to know. Learning test is written using Jasmine. Please refer Jasmine usage which I wrote before if you have interest it.
1. Javascript is dynamic typing programming language
It means that you don't need to specify type whenever declare variable. The type will be converted in run-time. In following example, We just use keyword "var" without specifying any type. It is also possible to use without explicit type casting. (Of course, programmer should careful when use it)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it("javascript is dynamically typed", function() { | |
var variable = 1; | |
expect(variable).toBe(1); | |
variable = "can assign string to same variable"; | |
expect(variable).toBe("can assign string to same variable"); | |
expect("5" - 5).toBe(0); | |
expect("5" + 5).toBe("55"); | |
expect("5" + "5").toBe("55"); | |
expect((+"5") + 5).toBe(10); | |
}); |
2. Variable
If you declare variable without "var", It means that you declare global variable. Please refer [line 9] , [line13] of following example closely.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it("define globla variables", function() { | |
globalVariable = 1; | |
expect(globalVariable).toBeDefined(); | |
}); | |
it("compare global variables and local variables", function() { | |
expect(globalVariable).toBe(1); | |
var localVariable = 2; | |
expect(localVariable).toBe(2); | |
(function() { | |
var localVariable; | |
expect(localVariable).not.toBe(2); | |
expect(localVariable).toBe(undefined); | |
})(); | |
}); |
3. undefined, null
If you just declare variable but not to assign any value, it will be treated as "undefined". But depend on context, you can see that undefined and null are converted dynamically. "undefined" is treated as NaN(Not a Number) in decimal context. But "null" is treated as 0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it("undefeind", function() { | |
var u; | |
expect(u).toBe(undefined); | |
expect(u == undefined).toBe(true); | |
expect(u == null).toBe(true); | |
expect(isNaN(u * 1)).toBe(true); | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it("null", function() { | |
var n = null; | |
expect(n).toBe(null); | |
expect(n == undefined).toBe(true); | |
expect(n == null).toBe(true); | |
expect(n * 1).toBe(0); | |
}) |
4. constant
The variable name and function name which are declared with "constant" cannot be redeclared.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it("constant cannot be re-declared", function() { | |
var throwFunc = function() { | |
const c = 1; | |
var c = 2; | |
} | |
expect(throwFunc).toThrowError(); | |
}); |
5. Array
Javascript Array is quite similar with general programming's array. Let's see [line 6]. If there are no value, it is treated as an undefined.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it("array literals", function() { | |
var aray = ['string', ,1]; | |
expect(aray.length).toBe(3); | |
expect(aray[0]).toBe('string'); | |
expect(aray[1]).toBeUndefined(); | |
expect(aray[2]).toBe(1); | |
}); |
6. Object
You need to see how to declare object and how to access its member closely. Like [line 9], If the member's name is decimal, You can use it like Array. But, if the name is not decimal, you should use like [line 4] or [line6]. You cannot use like [line 8]. It will cause javascript error.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it("object literals", function() { | |
var object = {"name":"javascript", 1:"html", 3:"html"}; | |
expect(object.name).toBe("javascript"); | |
expect(object[name]).toBe(undefined); | |
expect(object["name"]).toBe("javascript"); | |
expect(object[0]).toBe(undefined); | |
// expect(object.1).toBe("html"); | |
expect(object[1]).toBe("html"); | |
expect(object["1"]).toBe("html"); | |
expect(object[2]).toBe(undefined); | |
expect(object[3]).toBe("html"); | |
}); |
7. Reference
- MDN - https://developer.mozilla.org/en/docs/Web/JavaScript- Jasmine - http://jasmine.github.io/2.3/introduction.html
댓글 없음:
댓글 쓰기