分类目录归档:JavaScript

JavaScript变量类型检查

var str = 'str';
console.log(typeof str); // string
console.log(str.constructor); // String
function User(){}
var u = new User();
console.log(typeof u); // object
console.log(u.constructor); // User
Variable typeof Variable Variable.constructor
{an:"object"} object Object
["an","array"] object Array
function(){} function Function
"a string" string String
33 number Number
true boolean Boolean
new User() object User

JavaScript引用

JavaScript的一个重要的方面是引用的概念。引用就是指向对象实际位置的指针。这是一项极其强大的功能。前提是,实际的对象决不是一个引用:字符串总是一个字符串,数组总是一个数组。然而,多个变量可以引用相同的对象。JavaScript就是以这种引用机制为基础。通过维护一系列的指向其他对象的引用,语言为你提供了更大的弹性。
当几个变量指向相同对象时,修改这个对象会在所有指向这个对象的变量上有什么反应呢?

var obj = new Object();
var objRef = obj;
obj.someProp = true;
console.log(objRef===obj); // true
var arr = new Array(1,2);
var arrRef = arr;
arr.push(3);
console.log(arr === arrRef); // true
var str = "test";
var strRef = str;
str += "ing";
console.log(str === strRef); // false
var num = 1;
var numRef = num;
num += 1;
console.log(num === numRef); // false
var boo = true;
var booRef = boo;
boo = !boo;
console.log(boo === booRef); // false

我们知道string, number, bool是JavaScript的基础类型,Object, Array是组合类型,组合类型的值是以对象的属性存储的,所以改变了一个变量的值,所有指向它的变量都会变。

JavaScript原型继承


function A(){
  this.fa = function(){
    console.log("function in A");
  }
}
function B(){
  this.fb = function(){
    console.log("function in B");
  }
}
var a = new A();
a.fa();//function in A
var b = new B();
// b.fa(); //throw error;
b.fb(); //function in B
console.log(b instanceof A) //false
B.prototype = new A();
var b = new B();
b.fa(); //function in A
b.fb(); //function in B
console.log(b instanceof A)//true
console.log(b instanceof B)//true

JavaScript类型转换 type conversions

var a = new Boolean(false);
console.log(a);//{}
console.log(a == false)//true
console.log(a === false)//false
if(a){
    console.log("new Boolean(false) is true")//new Boolean(false) is true
}else{
    console.log("new Boolean(false) is false")
}

这里a转换为true,因为a是一个Boolean的object,而object如果不是null或者undefined,就会被转换为true
这里主要需要分清楚值类型(primitive)和对象类型(object)
下面是JavaScript类型转换

JavaScript type conversions
转换为:
String Number Boolean Object
undefined "undefined" NaN false throws TypeError
null "null" 0 false throws TypeError
true "true" 1

new Boolean(true)

false

"false"

0

new Boolean(false)

""?(empty string)

0

false

new String("")

"1.2"?(nonempty, numeric)

1.2

true

new String("1.2")

"one"?(nonempty, non-numeric)

NaN

true

new String("one")

0

"0"

false

new Number(0)

-0

"0"

false

new Number(-0)

NaN

"NaN"

false

new Number(NaN)

Infinity

"Infinity"

true

new Number(Infinity)

-Infinity

"-Infinity"

true

new Number(-Infinity)

1?(finite, non-zero)

"1"

true

new Number(1)

{}?(any object)

true

[]?(empty array)

""

0

true

[9]?(1 numeric elt)

"9"

9

true

['a']?(any other array)

use join() method

NaN

true

function(){}?(any function)

NaN true

javascript:null and undefined

声明而没有赋值的变量是undefined
没有返回值的函数返回的是undefined

var a;//undefined
console.log(a);//undefined
console.log(typeof a);//undefined
console.log(typeof a == undefined)//false
console.log(typeof a == "undefined")//true
console.log(typeof a === undefined)//false
console.log(typeof a === "undefined")//true
console.log(a==null);//true
console.log(a==undefined);//true
console.log(a===null);//false
console.log(a===undefined);//true
console.log("*******************");
var b = null;//null
console.log(b);//null
console.log(typeof b);//object
console.log(typeof b == undefined)//false
console.log(typeof b == "undefined")//false
console.log(typeof b === undefined)//false
console.log(typeof b === "undefined")//false
console.log(b==null);//true
console.log(b==undefined);//true
console.log(b===null);//true
console.log(b===undefined);//false

How to Create a Javascript Console in Sublime Text – wikiHow

Two Methods:Using JSC (Mac OS X)Using Node.js

Javascript consoles are very handy for debugging and getting live results from your script. Although Sublime Text comes with build systems for many other scripting languages, it does not come with a built-in Javascript build system. Many sources will tell you to create a .html page with a link to the .js file, then use a web browser console to see the results of your code. This equates to constant window-switching and browser reloading; leading to frustration, heartache, and ultimately inefficiency.

 

Fortunately, constructing your own Javascript build system for Sublime Text is quick and easy!

Method 1 of 2: Using JSC (Mac OS X)

JSC is a command-line Javascript runner, cooked directly into Mac OS X. Because your Mac already contains everything you need to run the script, creating the build system in Sublime Text is incredibly easy. (If you have a Windows computer, see the directions for Node.js below.)

Creating The Build System

  1. 1

    Launch Sublime Text.

  2. ToolsBuildSytemCreate.png
    2

    Go to “Tools > Build System > New Build System” in the top bar.

  3. Screen Shot 2013 10 26 at 10.10.27.png
    3

    Paste this code into the resulting new tab that Sublime Text opened, replacing anything else in it:

    {
      "cmd":["/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc","$file"],
      "selector":"source.js"
    }
    
  4. JSCSave.png
    4

    Save the file as “JSC.sublime-build” in the default “user” folder.?Now you have created your build system!

Usage

  1. 1

    Open the Javascript file that you want to run in Sublime Text.

  2. 2

    Use?debug()?instead of?console.log()?in your script.

  3. ChooseJSC.png
    3

    Go to “Tools > Build System” in the top bar and select “JSC”.?This is the build system that you just created.

  4. JSCBuilded4.png
    4

    Build the Javascript file, using either the shortcut ?B, or by choosing “Build” from the “Tools” menu.?A console will now appear in a pane at the bottom of the window, showing the results of your script!

Method 2 of 2: Using Node.js

Node.js (Node) is a platform built to allow Javascript to run on a server. However, it can also be installed on your local computer, providing a relatively simple way to run Javascript and get the results without using a browser.

  1. 1

    Download the Node installer from the?project’s homepage?and run it.?Simply use the default settings.

  2. ToolsBuildSytemCreate.png
    2

    Go to “Tools > Build System > New Build System” in the top bar.

  3. Creatingnodebuilder.png
    3

    Paste this code into the resulting new tab that Sublime Text opened, replacing anything else in it:

    {
      "cmd":["node","$file"],
      "selector":"source.js"
    }
    
  4. NodeSave.png
    4

    Save the file as “node.sublime-build” in the default “user” folder.?Now you have created your build system!

Usage

  1. 1

    Open the Javascript file that you want to run in Sublime Text.

  2. ChooseNode.png
    2

    Go to “Tools > Build System” in the top bar and select “node”.?This is the build system that you just created.

  3. NodeBuilded1.png
    3

    Build the Javascript file, using either the build shortcut (Ctrl + B for Windows, and ? + B for Mac), or by choosing “Build” from the “Tools” menu.?A console will now appear in a pane at the bottom of the window, showing the results of your script!