`
DBear
  • 浏览: 231031 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类

JavaScript学习笔记

阅读更多
1、JavaScript does not support multidimensional arrays,except as arrays of arrays.

2、JavaScript is an untyped language, the elements of an array do not all need to be of the same type, as they do in typed languages like Java.

3、Arrays can be created with the Array( ) constructor function.
   var a = new Array( );
   a[0] = 1.2;
   a[1] = "JavaScript";
   a[2] = true;
   a[3] = { x:1, y:3 };

4、Arrays can also be initialized by passing array elements to the Array( ) constructor.   var a = new Array(1.2, "JavaScript", true, { x:1, y:3 });

5、If you pass only a single number to the Array( ) constructor, it specifies the length of the array. Thus:
   var a = new Array(10);
   creates a new array with 10 undefined elements.

6、Also, as with object literals, the elements in array literals can be arbitrary expressions and need not be restricted to constants:
   var base = 1024;
   var table = [base, base+1, base+2, base+3];

7、The JavaScript keyword null is a special value that indicates no value. null is usually considered a special value of object typea value that represents no object.

8、When null is used in a Boolean context, it converts to false. When used in a numeric context, it converts to 0. And when used in a string context, it converts to "null".

9、Another special value used occasionally by JavaScript is the undefined value. undefined is returned when you use either a variable that has been declared but never had a value assigned to it or an object property that does not exist. Note that this special undefined value is not the same as null.

10、Although null and the undefined value are distinct, the == equality operator considers them equal to one another. However, if you truly must distinguish between a null value and an undefined value, use the === identity operator or the typeof operator.

11、When the undefined value is used in a Boolean context, it converts to false. When used in a numeric context, it converts to NaN. And when used in a string context, it converts to "undefined".

12、JavaScript can flexibly convert values from one type to another. When you use a string in an object contexti.e., when you try to access a property or method of the stringJavaScript internally creates a String wrapper object for the string value. This String object is used in place of the primitive string value. The object has properties and methods defined, so the use of the primitive value in an object context succeeds.

13、If both values are numbers and have the same value, they are identical, unless either or both values are NaN, in which case they are not identical. The NaN value is never identical to any other value, including itself! To check whether a value is NaN, use the global isNaN( ) function.

14、If one operand is or converts to a string, and one is or converts to a number, the operator attempts to convert the string to a number and performs a numerical comparison. If the string does not represent a number, it converts to NaN, and the comparison is false. (In JavaScript 1.1, the string-to-number conversion causes an error instead of yielding NaN.),for example:
  var str1 = "notANumber";
  var int1 = 1234;
  var test1 = (str1 < int1); //return false;
  var test2 = (str1 > int1); //return false;
  var test3 = (str1 == int1); //return false;
So we can not compare a string with a number as following:
  if (str1 < int1) {
    document.write("str1 < int1");
  } else {
    document.write("str1 >= int1");
  }
The result is obviously wrong.Thus, we should check the string with isNaN before comparison.

15、If the operands of the comparison operators cannot both be successfully converted to numbers or to strings, these operators always return false.

16、If either operand is or converts to NaN, the comparison operator always yields false.

17、When combining JavaScript and HTML, it is a good idea to use one style of quotes for JavaScript and the other style for HTML.
<a href="" onclick="alert('Thank you')">Click Me</a>

18、In some implementations of JavaScript, individual characters can be read from strings (but not written into strings) using array notation, so the earlier call to charAt( ) can also be written like this:
last_char = s[s.length - 1];
Note, however, that this syntax is not part of the ECMAScript v3 standard, is not portable, and should be avoided.

19、If a number is used where a boolean value is expected, the number is converted to TRue unless the number is 0 or NaN, which are converted to false. If a string is used where a boolean value is expected, it is converted to true except for the empty string, which is converted to false. null and the undefined value convert to false, and any non-null object, array, or function converts to true.

20、The fact that functions are true values in JavaScript gives a lot of flexibility to the language. It means that functions can be stored in variables, arrays, and objects, and it means that functions can be passed as arguments to other functions.

21、Objects in JavaScript can serve as associative arrays
image["width"]
image["height"]

22、The property values used in object literals need not be constants; they can be arbitrary JavaScript expressions. Also, the property names in object literals may be strings rather than identifiers:

var square =
{ "upperLeft": { x:point.x, y:point.y },
  'lowerRight': { x:(point.x + side), y:(point.y+side) }};

23、typeof(i)
   primitive: "number", "string", "boolean"
   arrays, null, String, Number, Boolean, Date,
   RegExp, objects, all client-side objects: "object"
   function: "function"
   undefined: "undefined"

24、typeof is useful only to distinguish objects from other, primitive types. In order to distinguish one object type from another, you must use other techniques, such as the instanceof operator.

25、The most common use for ‘void’ is in a client-side javascript: URL, eg.
   <a href="javascript:void window.open( );">Open New Window</a>
  When a new window is created, the function will return the window object(the new one) if nothing else. And since this code is called from href attribute, the browser will attempt to go to that returned text, which is the object's toString result:object [object]. So a blank screen usually appears with just that text on it(your original page disappears). Using void operator means that no return is passed to the href.In fact that javascript pseudo-URL technique is obsolete and is best to use:
  <a href="#" onclick="window.open( ); return false;">
  Open New Window</a>
  Besides which, if you use the void() method, and your viewer happens to right-click on the link, and then chooses "Open link in a new window" - a nasty error results. Using onClick() instead at least avoids the error.

26、For 'switch' statement, the matching case is determined using the === identity operator, not the == equality operator, so the expressions must match without any type conversion.

27、for (variable in object)
      statement

  Before the body of the loop is executed, the name of one of the object's properties is assigned to variable, as a string. Note that it is the properties' name, not the values. The variable in the for/in loop may be an arbitrary expression, this expression is evaluated each time through the loop, which means that it may evaluate differently each time. For example, you can use code like the following to copy the names of all object properties into an array:
var o = {x:1, y:2, z:3};
var a = new Array( );
var i = 0;
for(a[i++] in o) /* empty loop body */;
document.write(a[1])
//array a has been evaluated, and a[1] is 'y' not 2

28、function definitions occur at parse time rather than at runtime(parse time is earlier than runtime ).Consider the following code:

alert(f(4));     // Displays 16. f( ) can be called before it is defined.
var f = 0;       // This statement overwrites the property f.
function f(x) {  // This "statement" defines the function f before either
    return x*x;  // of the lines above are executed.
}
alert(f);        // Displays 0. f( ) has been overwritten by the variable f.


29、Array and Object
  /* Array is a special object, so it can have properties */
  var a = [1, 2, 3, 4];
  a["new"] = 16;
  document.write(a.length); 
//4, a["new"] is not a new element of array, but a property
  document.write(a.new);  //16
 
  /* Adding array elements to an object does not make it an array */

  var o = {x:1, y:2};
  o[0] = 21;
  document.write(o.length);
//undefined
  document.write(o.0);  //error
  document.write(o[0]);  //21
分享到:
评论

相关推荐

    个人Javascript学习笔记 精华版

    个人Javascript学习笔记 精华版 本资源为个人Javascript学习笔记的精华版,涵盖了Javascript的基础知识、事件处理、对象和系统函数、浏览器对象等方面的内容。下面是对每个知识点的详细说明: 1. 什么是JavaScript...

    javascript学习笔记

    ### JavaScript学习笔记精要 #### JavaScript简介 JavaScript是一种强大的、多用途的脚本语言,用于增强网站的交互性和用户体验。它是由Netscape公司的Brendan Eich在1995年发明的,并且迅速成为了Web开发的标准之...

    javascript学习笔记讲解版参考.pdf

    JavaScript学习笔记讲解版参考.pdf是一份详尽的教程,涵盖了从基础到进阶的JavaScript知识。这份笔记首先从CSS样式表开始,引导读者理解网页样式的设置与应用。 1. CSS(Cascading Style Sheets)样式表是用于控制...

    javascript学习笔记整理知识点整理

    这份“javascript学习笔记整理知识点整理”是针对初学者的一份宝贵资料,涵盖了JavaScript的基础知识,旨在帮助新手快速入门并掌握这门语言的核心概念。 一、变量与数据类型 在JavaScript中,变量用于存储数据。...

    JavaScript学习笔记,javascript基础知识,基础语法整理.pdf

    JavaScript基础知识点总结 JavaScript是一种高级的、动态的、基于对象的客户端脚本语言。它是在网页上执行的脚本语言,能实现网页的交互功能。下面是该资源中的重要知识点总结: 一、 JavaScript 基本概念 * ...

    JavaScript学习笔记.pdf

    JavaScript学习笔记是一本关于JavaScript编程语言的教材,该教材通过丰富的实例,系统地介绍了JavaScript的基础知识和实际应用技巧,帮助读者一步步掌握客户端编程技术。本书共分为九章,每一章都有其特定的主题,...

    我的javascript学习笔记

    根据提供的文件信息,可以看出这份“我的javascript学习笔记”主要涵盖了JavaScript中的几个关键概念和技术要点,包括正则表达式、AJAX以及一些JavaScript的核心语言特性。接下来将这些知识点进行详细的整理和解释。...

    JavaScript 学习笔记集和代码库

    JavaScript 学习笔记集和代码库JavaScript 学习笔记集和代码库JavaScript 学习笔记集和代码库JavaScript 学习笔记集和代码库JavaScript 学习笔记集和代码库JavaScript 学习笔记集和代码库JavaScript 学习笔记集和...

    Javascript学习笔记___自学实用

    JavaScript学习笔记——深入理解基础与函数 在JavaScript中,学习基础知识是至关重要的,因为它是所有进一步编程技巧的基础。首先,我们需要了解JavaScript中的数据类型。在JavaScript中,有五种简单的数据类型:...

    Javascript学习笔记(传智播客视频学习笔记+代码)

    "Javascript学习笔记(传智播客视频学习笔记+代码)"是一份全面介绍JavaScript基础知识的学习资源,适用于初学者。这份笔记结合了传智播客的web前端培训视频内容,提供了丰富的理论讲解和实践代码,帮助读者从零开始...

    JavaScript学习笔记讲解

    这只是JavaScript学习笔记的一小部分,JavaScript还有更多高级特性和概念,如对象、数组、函数、类、模块、闭包等,以及DOM操作、事件处理、Ajax异步请求等内容,需要进一步深入学习和实践才能掌握。

    javaScript学习笔记.rar

    这个“javaScript学习笔记.rar”压缩包显然包含了作者在学习JavaScript过程中的心得和记录,对于初学者或者想要深入理解JavaScript的人来说,是一份宝贵的资源。 JavaScript与Java虽然名字相似,但两者实际上是不同...

    蓝杰JavaScript学习笔记

    《蓝杰JavaScript学习笔记》是一份综合性的JavaScript学习资料,主要涵盖了JavaScript在网页动态操作、DOM操作以及事件处理等方面的基础知识。这篇笔记通过多个实例文件,如`dynamicCreateTable.htm`、`...

Global site tag (gtag.js) - Google Analytics