`
dyccsxg
  • 浏览: 201789 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
社区版块
存档分类

JavaScript 常用功能代码

阅读更多

1. 使用 iframe 时,父窗体与子窗体之间的相互调用

// 父窗体调用子窗体内的函数
window.frames['ifm_id'].valueChange("id_101");
// 子窗体调用父窗体的函数
parent.refreshTree("nodeId_202");

 

2. 弹出窗体与返回值

// 弹出窗体
var url = "http://www.baidu.com";
win=window.showModalDialog(url,window,"dialogLeft:400;dialogTop:200;dialogWidth:560px;dialogHeight:380px;scroll:yes;menubar:no;toolbar:no;status:no;");
// 在弹出窗体中设置返回值
var result = new Array();
result[0] = "id_101";
result[1] = "name_202";
window.returnValue = result;
window.close();

 

3. javascript 作用域[只有全局作用域和函数作用域,javascript没有块作用域]

// 1. 全局作用域
var id = "global variable";    // 1.1 在函数外部定义的变量
function showMsg(){    
    message = "global message";// 1.2 未定义而直接赋值的变量
                               //     在第一次使用时被定义为全局变量
}
// 2. 函数作用域
function doCheck(){
    var data = "function data";// 2.1 在函数内部定义的变量
}

 

4. javascript 继承机制

// 1. 对象冒充继承
function Person(strName){
    // private fields
    var name = strName;
    // public methods
    this.getName = function(){
        return name;
    };    
}
function Student(strName,strSchool){
    // 定义父类的属性及方法    
    this.parent = Person;
    this.parent(strName);
    delete this.parent;        // 删除临时变量 parent
    // 定义新属性及方法    
    // private fields
    var school = strSchool;
    // public methods
    this.getSchool = function(){
        return school;
    };     
}
// 2. Funtion 对象的 call(..) 或 apply(..) 继承
//    call 和 apply 的区别在于:
//      call  的第二个参数为可变参数;
//      apply 的第二个参数为 Array;
function Animal(strName,intAge){
    // private fields
    var name = strName;
    var age = intAge;
    // public methods
    this.getName = function(){
        return name;
    }; 
    this.getAge = function(){
        return age;
    };
}
function Cat(strName,intAge,strColor){
    // 定义父类的属性及方法    
    Animal.call(this,strName,intAge);
    // Animal.apply(this,new Array(strName,intAge));
    // 定义新属性及方法    
    // private fields
    var color = strColor;
    // public methods
    this.getInfo = function(){
        return "name:" + this.getName() + "\n"
             + "age:" + this.getAge() + "\n"
             + "color:" + color;
    };
}
// 3. prototype 继承
//    prototype 声明的属性及方法被所有对象共享
//    prototype 只有在读属性的时候会用到
Function.prototype.extend = function(superClass){
    // 此处的 F 是为了避免子类访问父类中的属性 this.xxx
    function F(){};
    F.prototype = superClass.prototype;
    // 父类构造函数
    this.superConstructor = superClass;
    this.superClass = superClass.prototype;
    this.prototype = new F();
    this.prototype.constructor = this;
};
Function.prototype.mixin = function(props){    
    for (var p in props){        
        this.prototype[p] = props[p];        
    }
};
function Box(){}
Box.prototype = {    
    getText : function(){
        return this.text;
    },
    setText : function(text){
        this.text = text;
    }
};
function CheckBox(){}
CheckBox.extend(Box);
CheckBox.mixin({
    isChecked : function(){
        return this.checked;
    },
    setChecked : function(checked){
        this.checked = checked;
    }
});

 

5. call , apply & bind

// thisArg 表示在 fun 内部时 this 所指示的对象
// call & apply 将立即执行 fun 并返回结果
var result = fun.call(thisArg,arg1,...);
var result = fun.apply(thisArg,[argsArray]);
// thisArg 表示在 fun 内部时 this 所指示的对象
// bind 返回的是一个匿名函数
var tmpfun = fun.bind(thisArg);
var result = tmpfun(arg1,...);

 

<script type="text/javascript">
/**
 * 扩展 Function 的功能
 */
Function.prototype.bind = function(obj){
    var method = this;
    var tmpfun = function(){
        return method.apply(obj,arguments);
    };
    return tmpfun;
}
function Parent(){
    this.name = "parent";
}
function Child(){
    this.name = "child";
    this.getName = function(time){
        return time + " " + this.name;
    };
}
var parent = new Parent();
var child = new Child();
alert(child.getName(1));                // show 1 child
alert(child.getName.call(parent,2));    // show 2 parent [call & apply 会立即执行]
var tmpfun = child.getName.bind(parent);// bind 不会立即执行
alert(tmpfun(3));                       // show 3 parent
</script>

 

6. js "==" Operator

转换规则
   如果一个操作数是 Boolean 值,则比较之前将其转成数字:false -> 0, true -> 1;
   如果一个操作数是数字,另一操作数是字符串,则比较之前将字符串转成数字;
   如果一个操作数是对象,另一操作数是数字或字符串,则比较之前会将对象转为基本类型,
       引擎会先尝试调用 valueOf(),如果 valueOf() 没有 override 或返回一个对象,
       则引擎会尝试调用 toString(),如果 toString() 没有 override 或返回一个对象,则抛出异常;
   如果是两个对象进行比较,则判断它们是否引用同一对象;
   如果一个操作数是 NaN, == 将返回 false, != 将返回 true;
   null 和 undefined 与其它值比较将返回 false,
       但 null == null, undefined == undefined, null == undefined;
   参与比较时 null 和 undefined 不能转为其它值;  

 

7. END.

 

附录:

1. Mozilla developer center JavaScript Reference
https://developer.mozilla.org/en/JavaScript/Reference

2. Methods
https://developer.mozilla.org/en/DOM/window#Methods

分享到:
评论

相关推荐

    常用javascript特效代码(带预览功能)

    资源名称:常用Javascript特效代码(带预览功能)   内容简介: 常用Javascript特效代码集锦,带有演示,左侧是特效分类及名称,点击后在右侧窗口中可预览效果,点右键可查看源代码,非常...

    分享100个直接可以拿来用的JavaScript实用功能代码片段

    把平时网站上常用的一些实用功能代码片段通通收集起来,方面网友们学习使用,利用好的话可以加快网友们的开发速度,提高工作效率。

    javascript 常用代码大全

    4.2 屏蔽所有功能键 4.3 --&gt; 和,f9,f1 4.4 屏蔽组合键ctrl+n 5、网页设计类 5.1 连续滚动的文字,图片(注意是连续的,两段文字和图片中没有空白出现) 5.2 html编辑控件类 5.3 颜色选取框控件 5.4 ...

    Javascript代码美化工具

    JS Beauty 是一款 Javascript 美化工具,它具有美化,净化,压缩和解压缩 Javascript 代码等功能: 1. 美化:将混乱的 Javascript 代码格式化为优美的带缩进的格式,适合阅读和修改。 2. 净化:去掉 Javascript ...

    100个直接可以拿来用的JavaScript实用功能代码片段(1-10)

    90、原生JavaScript常用的正则表达式大收集 91、原生JavaScript实现窗体改变事件resize的操作(兼容所以的浏览器) 92、原生JavaScript用正则清除空格分左右 93、原生JavaScript判断变量是否空值 94、原生JavaScript...

    网页设计常用Javascript经典代码

    这些是常用的JS代码,设计网页或者写点小功能的时候可能会用到,来源互联网,自己整理了下,有不对的地方请大家多多指教!

    javascript代码常用大全

    4.2 屏蔽所有功能键 4.3 --&gt; 和,F9,F1 4.4 屏蔽组合键ctrl+N 5、网页设计类 5.1 连续滚动的文字,图片(注意是连续的,两段文字和图片中没有空白出现) 5.2 html编辑控件类 5.3 颜色选取框控件 5.4...

    asp.net 网页常用Javascript代码

    asp.net编程中常需要编写一些代码完成特定功能,Javascript可以轻松帮你完成这些。

    javaScript一些常用功能

    javaScript一些常用功能代码 js属性代码 (10,1) type=button value=属性&gt; &lt;OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0&gt;&lt;/OBJECT&gt; 可能提示“权限不足”

    【JavaScript源代码】JavaScript实现表单验证功能.docx

     本文实例为大家分享了JavaScript实现表单验证功能的具体代码,供大家参考,具体内容如下 以下是JavaScript的表单验证功能,可根据JS代码编写出你想要的HTML和CSS的代码。  关于正则表达式的使用,以及常用的正则...

    javascript常用代码大全.html

    4.2 屏蔽所有功能键 4.3 --&gt; 和,F9,F1 4.4 屏蔽组合键ctrl+N 5、网页设计类 5.1 连续滚动的文字,图片(注意是连续的,两段文字和图片中没有空白出现) 5.2 html编辑控件类 5.3 颜色选取框控件 5.4 下拉菜单 ...

    JavaScript 复制功能代码 兼容多浏览器(ZeroClipboard)

    IE 的 Flash JavaScript 通信接口上有一个 bug 。你必须插入一个 object 标签到一个已存在的 DOM 元素中。并且在写入 innerHTML 之前请确保该元素已经 appendChild 方法插入到 DOM 中。 Zero Clipboard 事件处理 ...

    javascript常用代码

    主要是以下功能的代码: 一、验证类 1、数字验证内 1.1 整数 1.2 大于0的整数 (用于传来的id的验证) 1.3 负整数的验证 1.4 整数不能大于imax 1.5 整数不能小于imin 2、时间类 2.1 短时间,形如 (13:...

    JavaScript完全自学宝典 源代码

    1.本书1~21章所附代码的运行环境 操作系统:Windows 2003、Windows XP Professional,或者Windows 2000 开发环境:UltraEdit12.10a、eclipse-jee-europa Web浏览器:Microsoft Internet Explorer 6.0及以上版本 ...

    javascript常用对象梳理

    JS中的常用对象[转载]web 技术 2010-06-05 15:00:30 阅读3 评论0 字号:大中小 订阅 [removed] Window For JavaScript 熟练window对象的open、close、alert、confirm、prompt、setTimeout、clearTimeout、...

    WebGIS从基础到开发实践代码(基于ArcGIS API for JavaScript)

    1.2.4OWS中的常用服务 1.2.5服务的请求与响应 1.3REST及REST风格的Web服务 1.3.1REST中的基础知识 1.3.2REST风格的Web服务 1.3.3REST风格的Web服务实例 1.4Web GIS的组成 1.5ArcGIS Server REST风格的Web服务 1.5.1...

    JavaScript 代码压缩加密软件

    产生的代码兼容IE,FireFox等常用浏览器。本软件可免费使用和转载,但严禁用于商业用途。 本软件不同于网络上已有的同类软件,现有的都是网页版的,本软件是完全的绿色软件,下载后直接运行即可,界面很简单。包括...

    很全面的JavaScript常用功能汇总集合

    本文主要总结了JavaScript 常用功能总结,如一些常用的额JS 对象,基本数据结构,功能函数等,还有一些常用的设计模式。  目录: 众所周知,JavaScript是动态的面向对象的编程语言,能够实现以下效果: 丰富Web ...

    107个常用Javascript语句

    107个常用Javascript语句,如果你写代码时想实现某个功能,动又不知道有没有这种功能的语法,请查看一下吧!

Global site tag (gtag.js) - Google Analytics