JS笔记(1) 基础概念

1.console.log用法,这个新手,比如我容易找不到console,细心点,在浏览器下方的选项卡里面,下面测试(ps:要用firebug)

<script>
var me = 'Felix'
console.log('hello word',me,",haha");
</script>

输出结果:hello word Felix ,haha

// Anything following double slashes is an English-language comment.
// Read the comments carefully: they explain the JavaScript code.
// variable is a symbolic name for a value.
// Variables are declared with the var keyword:
var x;                     // Declare a variable named x.
// Values can be assigned to variables with an = sign
x = 0;                     // Now the variable x has the value 0
x                          // => 0: A variable evaluates to its value.
// JavaScript supports several types of values
x = 1;                     // Numbers.
x = 0.01;                  // Just one Number type for integers and reals.
x = "hello world";         // Strings of text in quotation marks.
x = 'JavaScript';          // Single quote marks also delimit strings.
x = true;                  // Boolean values.
x = false;                 // The other Boolean value.
4    |    Chapter 1: Introduction to JavaScriptx = null;                  // Null is a special value that means "no value".
x = undefined;             // Undefined is like null.


需要注意的知识点:

1.用var定义变量,用等号赋值。

2.js支持的类型:整型,实数类型,文本字符串(可以用双引号或者单引号),布尔类型,无值类型(null/undefined)类型。

3.对象类型的属性、或者数组的元素都可以通过"."或者[""]访问。初始化object类型用={};如果{}里面无内容,表示空对象(没有属性的对象),但是可以用赋值的方法增加属性,如ob.newproperty = "test";

4.数组类型,初始化用=[1,2]的类似形式实现。定义和一般的类型相同,可以用primes.length获取数组的元素个数。访问最后一个元素是primes[primes.length-1]注意不要越界。

5.对象和数组可以存放其他对象和数组。

var points = [             // 存放两个对象
    {x:0, y:0},             
    {x:1, y:1}
];
1 var data = {               // 本对象有两个属性
2   trial1: [[1,2], [3,4]],  // 每个对象的值都是数组
3   trial2: [[2,3], [4,5]]   // 数组里面的元素还是一个数组
4 };

6.函数的定义和赋参数是用 function name(paras){}格式,函数可以赋值到变量。如下面代码:

1 var square = function(x) { // Functions are values and can be assigned to vars
2     return x*x;            // Compute the function's value
3 };                         // Semicolon marks the end of the assignment.
4 square(plus1(y))           // => 16: invoke two functions in one expression

7.当我们吧函数(function)结合到对象,我们叫他对象的“方法(method)”。

1 // When functions are assigned to the properties of an object, we call
2 // them "methods".  All JavaScript objects have methods:
3 var a = [];                // Create an empty array
4 a.push(1,2,3);             // The push() method adds elements to an array
5 a.reverse();               // Another method: reverse the order of elements

8.我们可以定义自己的方法,用this关键字指向它所在的函数区域对应的变量。

points.dist = function() { // Define a method to compute distance between points
    var p1 = this[0];      // First element of array we're invoked on
    var p2 = this[1];      // Second element of the "this" object
    var a = p2.x-p1.x;     // Difference in X coordinates
    var b = p2.y-p1.y;     // Difference in Y coordinates
    return Math.sqrt(a*a + // The Pythagorean theorem
                     b*b); // Math.sqrt() computes the square root
};
points.dist()              // => 1.414: distance between our 2 points

另一种用对象的实现

//定义一个构造函数,可以初始化一个point对象  
function Point(x,y) {      //按照惯例,构造函数以大写字母开始    this.x = x;            //this 关键字指向新初始化的对象
    this.y = y;            // Store function arguments as object properties
}                          // No return is necessary
// Use a constructor function with the keyword "new" to create instances
var p = new Point(1, 1);   // The geometric point (1,1)
// Define methods for Point objects by assigning them to the prototype
// object associated with the constructor function.
Point.prototype.r = function() {  
    return Math.sqrt(      // Return the square root of x² + y²
        this.x * this.x +  // This is the Point object on which the method...
        this.y * this.y    // ...is invoked.
    );
};
// Now the Point object p (and all future Point objects) inherits the method r()
p.r()                      // => 1.414...
原文地址:https://www.cnblogs.com/shenfengok/p/2577711.html