1.json转字符串
function json2str(o) {
var arr = [];
var fmt = function (s) {
if (typeof s == 'object' && s != null) return json2str(s);
return /^(string|number)$/.test(typeof s) ? "'" + s + "'" : s;
};
for (var i in o) arr.push("'" + i + "':" + fmt(o[i]));
return '{' + arr.join(',') + '}';
}
2.时间戳转为Date
function fromUnixTime(timeStamp) {
if (!timeStamp || timeStamp var theDate = new Date(parseInt(timeStamp) * 1000);
return theDate;
}
3.Data-format
// 作者: meizz
// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2012-12-02 08:12:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2012-12-02 8:12:4.18
Date.prototype.Format = function(fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
};
4.日期上增加n天
function addDay(number) {
return fromUnixTime(new Date().getTime() / 1000 + 24 * 60 * 60 * number);
}
5. 使用 iframe 时,父窗体与子窗体之间的相互调用
// 父窗体调用子窗体内的函数
window.frames['ifm_id'].valueChange("id_101");
// 子窗体调用父窗体的函数
parent.refreshTree("nodeId_202");
6. 弹出窗体与返回值
// 弹出窗体
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();
7. 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 在函数内部定义的变量
}
8. 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;
}
});
9. 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,...);
10. 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 不能转为其它值;

JavaScript是现代网站的核心,因为它增强了网页的交互性和动态性。1)它允许在不刷新页面的情况下改变内容,2)通过DOMAPI操作网页,3)支持复杂的交互效果如动画和拖放,4)优化性能和最佳实践提高用户体验。

C 和JavaScript通过WebAssembly实现互操作性。1)C 代码编译成WebAssembly模块,引入到JavaScript环境中,增强计算能力。2)在游戏开发中,C 处理物理引擎和图形渲染,JavaScript负责游戏逻辑和用户界面。

JavaScript在网站、移动应用、桌面应用和服务器端编程中均有广泛应用。1)在网站开发中,JavaScript与HTML、CSS一起操作DOM,实现动态效果,并支持如jQuery、React等框架。2)通过ReactNative和Ionic,JavaScript用于开发跨平台移动应用。3)Electron框架使JavaScript能构建桌面应用。4)Node.js让JavaScript在服务器端运行,支持高并发请求。

Python更适合数据科学和自动化,JavaScript更适合前端和全栈开发。1.Python在数据科学和机器学习中表现出色,使用NumPy、Pandas等库进行数据处理和建模。2.Python在自动化和脚本编写方面简洁高效。3.JavaScript在前端开发中不可或缺,用于构建动态网页和单页面应用。4.JavaScript通过Node.js在后端开发中发挥作用,支持全栈开发。

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。 1)C 用于解析JavaScript源码并生成抽象语法树。 2)C 负责生成和执行字节码。 3)C 实现JIT编译器,在运行时优化和编译热点代码,显着提高JavaScript的执行效率。

JavaScript在现实世界中的应用包括前端和后端开发。1)通过构建TODO列表应用展示前端应用,涉及DOM操作和事件处理。2)通过Node.js和Express构建RESTfulAPI展示后端应用。

JavaScript在Web开发中的主要用途包括客户端交互、表单验证和异步通信。1)通过DOM操作实现动态内容更新和用户交互;2)在用户提交数据前进行客户端验证,提高用户体验;3)通过AJAX技术实现与服务器的无刷新通信。

理解JavaScript引擎内部工作原理对开发者重要,因为它能帮助编写更高效的代码并理解性能瓶颈和优化策略。1)引擎的工作流程包括解析、编译和执行三个阶段;2)执行过程中,引擎会进行动态优化,如内联缓存和隐藏类;3)最佳实践包括避免全局变量、优化循环、使用const和let,以及避免过度使用闭包。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Dreamweaver CS6
视觉化网页开发工具

WebStorm Mac版
好用的JavaScript开发工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。