JavaScript 常用功能代码

本文介绍了JavaScript中的各种实用技巧,包括iframe窗体间通信、弹出窗体返回值、作用域规则、继承机制以及call、apply和bind的用法。通过这些技巧,开发者可以更好地理解和运用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

(SCI三维路径规划对比)25年最新五种智能算法优化解决无人机路径巡检三维路径规划对比(灰雁算法真菌算法吕佩尔狐阳光生长研究(Matlab代码实现)内容概要:本文档主要介绍了一项关于无人机三维路径巡检规划的研究,通过对比2025年最新的五种智能优化算法(包括灰雁算法、真菌算法、吕佩尔狐算法、阳光生长算法等),在复杂三维环境中优化无人机巡检路径的技术方案。所有算法均通过Matlab代码实现,并重点围绕路径安全性、效率、能耗和避障能力进行性能对比分析,旨在为无人机在实际巡检任务中的路径规划提供科学依据和技术支持。文档还展示了多个相关科研方向的案例与代码资源,涵盖路径规划、智能优化、无人机控制等多个领域。; 适合人群:具备一定Matlab编程基础,从事无人机路径规划、智能优化算法研究或自动化、控制工程方向的研究生、科研人员及工程技术人员。; 使用场景及目标:① 对比分析新型智能算法在三维复杂环境下无人机路径规划的表现差异;② 为科研项目提供可复现的算法代码与实验基准;③ 支持无人机巡检、灾害监测、电力线路巡查等实际应用场景的路径优化需求; 阅读建议:建议结合文档提供的Matlab代码进行仿真实验,重点关注不同算法在收敛速度、路径长度和避障性能方面的表现差异,同时参考文中列举的其他研究案例拓展思路,提升科研创新能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值