Gnatdiordna

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
  • 显示警告时,JavaScript脚本会被挂起。
  • 对象构造函数示例:

  

 1 function Card (name, address, work, home){
 2 
 3   this.name = name;
 4 
 5   this.address = address;
 6   
 7   this.work = work;
 8   
 9   this.home = home;
10 
11 }

 

  • 使用Prototype扩展内置对象,示例:
 1 function addhead(level){
 2 
 3  html = "H" +level;
 4 
 5  text = this.toString();
 6 
 7  start = "<" + html +">";
 8 
 9  end = "</" + html +">";
10 
11 return start + text + end;
12 
13 }
14 
15 String.prototype.heading = "head";
16 
17 document.write ("This is a heading 1".heading(1));
18 
19 //output: "<H1>This is a heading 1<H2>"

 

  • if( next > " ") statement ; 
注:将 next 与空格的进行大小比较,这里比较的是两者的 ASCII 码值大小,当大于空格时,可以粗略的认为是有有效输入(视应用场景)
  • 在JavaScript中,显示的字段用  '   ' 符号包围;
 
  • 使用循环navigator遍历对象属性:
1 for(i in navigator){
2     document.write("property:" + i);
3 
4     document.write(" value:" + navigator[i] + "<br>");
5 
6 }

 

window.navigator 对象包含有关访问者浏览器的信息;navigator 数据可被浏览器使用者更改;
Q: 当循环体结束时,i指向对象的下一个属性?
 
  • with 关键字:
引用《理解javascript中的with关键字》(http://www.jb51.net/article/79474.htm)的描述
with语句的作用是将代码的作用域设置到一个特定的作用域中,基本语法如下:
with (expression) statement;

这几行代码都是访问location对象中的属性,如果使用with关键字的话,可以简化代码如下:

 

1 with (location){
2 
3 var qs = search.substring(1);
4 
5 var hostName = hostname;
6 
7 var url = href;
8 
9 }

 

 

在这段代码中,使用了with语句关联了location对象,这就以为着在with代码块内部,每个变量首先被认为是一个局部变量,如果局部变量与location对象的某个属性同名,则这个局部变量会指向location对象属性。

在《JavaScript入门经典》中,提及的with作用为:
“with 关键字指定一个对象,后面跟着括在大括号中的一大块语句。对于块语句中的每一条语句,没有指定对象的属性都被假定为该对象的属性。举例来说,假定有一个名为lastname的字符串,可以用 with 来执行字符串操作,而不必每次都指定该字符串的名称:
1 with (lastname){
2 
3  window.alert("length of last name: " + length);
4 
5  capname = toUpperCase();
6 
7 }

 

在本例中,虽然只用 with 关键字指定了一次,但 length 属性和 toUpperCase 方法都会引用lastname字符串”
 
  • 一个事件调用多个函数的方法:
    1. 定义一个函数来调用多个函数,事件触发时调用“调用多个函数的函数”;
    2. 在IE6、7中用"attachEvent()"方法;在高级版本中用"addEventListener()"方法
 
 
  • Date() 方法相关:
JavaScript 中 Date 对象的四种创建格式示例:
birthday = new Day(); //不指定,其值为 new 对象时的时间
birthday = new Day("October 9,2016 23:03:00"); //通过字符串参数指定年月日时分秒
birthday = new Day(10, 9, 2016, 23, 3, 0);//通过数字参数指定年月日时分秒
birthday = new Day(10, 9, 2016);//通过数字参数指定年月日
getYear() 返回值为两位数的年份(如“16”),getFullYear() 返回值为四位数的年份(如“2016”),使用getFullYear() 可避免"2000年"问题。
 
  • <img>标签也有 onLoad 事件
 
  • event.which存储的是按键的ASCII码值;event.keyCode存储的是字符代码
 
  • 在form标签中,用onSubmit = "return validate()" 以达到条件前调用validate()方法进行验证的效果。当validate()返回值为true时,提交表单;当validate()返回值为false时,不提交表单。
 
  • 典型的AJAX运行机制:
    1. 脚本会首先创建一个XMLHttpRequest对象,然后将它发送给Web服务器。同时,脚本可以继续发送请求。
    2. 服务器会发送包含内容的文件(或服务端应用程序的输出)作为响应。
    3. 当接到来自服务器端的响应后,相应的JavaScript 函数将被触发,以处理相关数据。
    4. 由于引入AJAX的主要目的是为了获取更好的用户交互性,所以脚本通常会使用DOM显示来自服务器的数据,从而无需再次刷新页面。
实际上,这一过程执行的非常快。即使服务器的处理速度很慢也可正常地执行。此外,由于请求是异步的,所以可以同时处理多个请求
 
 
 

规范:
  • 内容是网页访问者在页面上阅读到的语句。通常以文本形式出现,并于HTML相结合,用于定义内容的类型——如标题、段落和链接等。
  • 展现是指页面中的外观和版面设计,这部分用 CSS来定义。
  • 行为是指与网页交互时发生的动作,由JavaScript来实现。
 
  • 渐进式提高:
    • 应该在独立的CSS文件中添加规则,增强展现的形式。避免在代码中直接使用HTML形式的展现标记符,例如用<b>来表示黑体。
    • 通过外部的JavaScript文件添加脚本来增强行为。
    • 用功能检测技术确保只有支持相应功能的浏览器才会执行对应的JavaScript代码。功能检测如下:
      //检测getElementById函数的存在
      if (document.getElementById){
      //dostuff
      }
      也可以在函数开头处使用:
      function changeText(){
      if(!document.getElementById)return;
      //the rest of the function executes if the feature is supported
      }
       
 

其它:
Math对象不能new,因为它是静态对象。
历元:以1970年1月1日子夜为起点计时的时间毫秒格式。
 
  • 循环变量i的来源:
SO的答案:
It comes ultimately from mathematics: the summation notation traditionally uses i for the first index, jfor the second, and so on. Example (from http://en.wikipedia.org/wiki/Summation):

\sum_{i=1}^{n} i = \frac{n^2 + n}{2}

It's also used that way for collections of things, like if you have a bunch of variables x1, x2, ... xn, then an arbitrary one will be known as xi.

As for why it's that way, I imagine SLaks is correct and it's because I is the first letter in Index.”
 
 
posted on 2016-10-11 01:38  Gnatdiordna  阅读(168)  评论(0)    收藏  举报