手写javascript,手写javascript的call apply bind
【前端面试:手写js系列】flat---数组扁平化
数组扁平化定义:在前端项目开发过程中,偶尔会出现层叠数据结构的数组,需要把多层数组转换为一级数组(即提取嵌套数组元素最终合并为一个数组),使其内容合并并且展开。
创新互联提供高防服务器租用、云服务器、香港服务器、成都IDC机房托管等
遍历数组的方案:
for循环
for...of
for...in
forEach()
entries()
keys()
values()
reduce()
map()
判断元素是否是数组的方案:
instanceof
constructor
object.prototype.toString.call
isArray
将数组元素进行展开一层的方案:
扩展运算法 + concat(concat() 方法用于合并两个或多个数组,在拼接的过程中加上扩展运算符会展开一层数组)
concat + apply(主要是利用 apply 在绑定作用域时,传入的第二个参数是一个数组或者类数组对象,其中的数组元素将作为单独的参数传给 func 函数。也就是在调用 apply 函数的过程中,会将传入的数组一个一个的传入到要执行的函数中,也就是相当对数组进行了一层的展开。)
toString + split(不推荐使用 toString + split 方法,因为操作字符串是很危险的事情,如果数组中的元素所有都是数字的话,toString + split 是可行的,并且是一步搞定。)
最终手写的flat()方法为:
参考博客如下,注明一下出处,感谢大神们,希望自己可以多多练习,多多回顾:
三元博客
JS数组reduce()方法详解及高级技巧
数组flat方法实现
能发一下手写输入的JS代码给我吗
给你JS,这个是用在opera下的手写输入,按住ctrl+鼠标左键呼出
——————————————————————————————
// ==UserScript==
// ==/UserScript==
SWFObject = function($) {
this.cfg = $;
this.swfId = $.fid;
this.asObjects = {};
this.onLoadInit = null;
SWFObject.instances[this.swfId] = this
};
SWFObject.prototype.load = function() {
Utils.f(this.cfg)
};
SWFObject.prototype.getASObject = function($) {
return this.asObjects[$.asoId]
};
SWFObject.prototype.registerASObject = function(A) {
var _ = A.asoId,
$ = this.swfId;
return this.asObjects[_] = new ASObject({
asoId: _,
swfId: $
})
};
SWFObject.prototype.getASObjectProperty = function($) {
if (this.swf == null) this.swf = Utils.g(this.swfId);
return this.swf.getASObjectProperty($)
};
SWFObject.prototype.setASObjectProperty = function($) {
if (this.swf == null) this.swf = Utils.g(this.swfId);
this.swf.setASObjectProperty($)
};
SWFObject.prototype.callASObjectMethod = function($) {
if (this.swf == null) this.swf = Utils.g(this.swfId);
return this.swf.callASObjectMethod($)
};
SWFObject.instances = {};
SWFObject.getSWFObject = function($) {
return SWFObject.instances[$.swfId]
};
SWFObject.dispatchASObjectEvent = function(B) {
var _ = B.swfId,
A = B.asoId,
F = B.type,
E = B.data,
C = SWFObject.getSWFObject({
swfId: _
}),
$ = (A == undefined) ? C: C.getASObject({
asoId: A
}),
D = "on" + F.substr(0, 1).toUpperCase() + F.substr(1);
if ($[D] != null) $[D](E)
};
SWFObject.registerASObject = function(A) {
var $ = A.swfId,
_ = A.asoId,
B = SWFObject.getSWFObject(A);
B.registerASObject(A)
};
ASObject = function($) {
this.swfId = $.swfId;
this.asoId = $.asoId;
this.swfObject = SWFObject.getSWFObject($);
if (ASObject.instances[this.swfId] == null) ASObject.instances[this.swfId] = {};
ASObject.instances[this.swfId][this.asoId] = this
};
ASObject.prototype.get = function($) {
return this.swfObject.getASObjectProperty({
asoId: this.asoId,
property: $
})
};
ASObject.prototype.set = function(_, $) {
this.swfObject.setASObjectProperty({
asoId: this.asoId,
property: _,
value: $
})
};
ASObject.prototype.call = function($, _) {
return this.swfObject.callASObjectMethod({
asoId: this.asoId,
method: $,
parameters: _
})
};
ASObject.instances = {};
Utils = {
g: function($) {
return document.getElementById($)
},
f: function(_) {
var $ = 'embed id="#{fid}" name="#{fid}" src="#{movie}" flashVars="#{flashVars}" width="#{width}" height="#{height}" align="#{align}" type="application/x-shockwave-flash" pluginspage="" wmode="#{wmode}" scale="#{scale}" salign="#{salign}" allownetworking="#{allownetworking}" allowscriptaccess="#{allowscriptaccess}" /',
A = 'object id="#{fid}" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="#{width}" height="#{height}" align="#{align}"param name="movie" value="#{movie}" /param name="flashVars" value="#{flashVars}" /param name="wmode" value="#{wmode}" /param name="scale" value="#{scale}" /param name="salign" value="#{salign}" /param name="allownetworking" value="#{allownetworking}" /param name="allowscriptaccess" value="#{allowscriptaccess}" //object';
this.g(_.cid).innerHTML = this.fm(this.b.isIE ? A: $, _)
},
fm: function($, _) {
if (arguments.length) if (typeof(_) == "object") $ = $.replace(/#\{([^\{\}]+)\}/g,
function(A, B) {
var $ = _[B];
if (typeof $ == "function") $ = $(B);
return typeof($) == "undefined" ? "": $
});
else if (typeof(_) != "undefined") for (var A = arguments.length - 2; A -1; A--) $ = $.replace(new RegExp("#\\{" + A + "\\}", "g"), arguments[A + 1]);
return $
},
b: {
isIE: /msie/i.test(navigator.userAgent),
isFF: /firefox/i.test(navigator.userAgent),
isMaxthon: (function() {
var $ = false;
try {
$ = external.max_version
} catch(_) {}
return $
})()
},
mo: function() {
var $ = {},
B = arguments.length;
for (var C = 0; C B; C++) {
var A = arguments[C];
for (var _ in A) {
if ($[_] != null) continue;
$[_] = arguments[C][_]
}
}
return $
}
};
HWR = {
loaded: false
};
HWR.load = function(E, C, D) {
HWR.tid = C;
var $ = D ? ("serviceUrl=" + D) : "",
B = {
cid: E,
fid: E + "_flash",
width: 410,
height: 290,
movie: "",
align: "middle",
salign: "lt",
wmode: "window",
scale: "noscale",
allowscriptaccess: "always",
allownetworking: "all",
flashVars: $
},
A = new SWFObject(B);
HWR.so = A;
var _;
A.onLoadInit = function($) {
HWR.ao = _ = A.asObjects.handWrite;
_.onCloseClick = function($) {
HWR._onCloseClick()
};
_.onCharSelect = function($) {
HWR._onCharSelect($["char"])
}
};
A.load()
};
HWR._onCharSelect = function(A) {
var $ = HWR.tid;
var G = $.selectionStart;
var H = $.selectionEnd;
$.value = $.value.substring(0,G) + A + $.value.substring(H,$.value.length);
$.selectionStart = G + A.length;
$.selectionEnd = G + A.length;
};
HWR._onCloseClick = function() {
if (HWR.onCloseClick) HWR.onCloseClick()
};
HWR.onCloseClick = null;
HWR.clear = function() {
HWR.ao.call("clear", [])
};
function getFlashVersion() {
var B = navigator;
if (B.plugins B.mimeTypes.length) {
var A = B.plugins["Shockwave Flash"];
if (A A.description) return A.description.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s)+r/, ".") + ".0"
} else if (window.ActiveXObject !window.opera) for (var C = 10; C = 2; C--) {
try {
var _ = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + C);
if (_) {
return C + ".0.0";
break
}
} catch($) {}
}
return "0"
}
function addStyle(A, $) {
var _ = document.styleSheets[0];
if (_.addRule) _.addRule(A, $);
else if (_.insertRule) _.insertRule(A + " { " + $ + " }", _.cssRules.length)
}
function penControl() {
var $ = parseInt(getFlashVersion());
if ($ 9) addStyle("#penControl", "display:none")
}
function stopClosePen($) {
$ = $ || window.event;
if ($.stopPropagation) $.stopPropagation();
else $.cancelBubble = true
}
var enablePen = false;
function openPen($) {
if (!enablePen) {
var A = document.getElementById(HWR.so.cfg.cid);
A.className = "hwr_visible_ujs";
enablePen = true
}
$ = $ || event;
var _ = $.target || $.srcElement;
_.blur();
stopClosePen($)
}
HWR.onCloseClick = function() {
HWR.clear();
var _ = document.getElementById(HWR.so.cfg.cid);
_.className = "hwr_hidden_ujs";
enablePen = false
};
function closePen($) {
var B = document.getElementById(HWR.so.cfg.cid);
$ = $ || event;
var A = $.target || $.srcElement;
if (enablePen) {
HWR.clear();
B.className = "hwr_hidden_ujs";
enablePen = false
}
}
function findPos(obj) {
var curleft = curtop = 0;
do {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
} while (obj = obj.offsetParent);
return [curleft, curtop];
}
function scrollDist() {
var html = document.getElementsByTagName('html')[0];
if (html.scrollTop document.documentElement.scrollTop) {
return [html.scrollLeft, html.scrollTop];
} else if (html.scrollTop || document.documentElement.scrollTop) {
return [html.scrollLeft + document.documentElement.scrollLeft, html.scrollTop + document.documentElement.scrollTop];
} else if (document.body.scrollTop)
return [document.body.scrollLeft, document.body.scrollTop];
return [0, 0];
}
function innerDimensions() {
if (self.innerHeight) {
return [self.innerWidth, self.innerHeight];
} else if (document.documentElement document.documentElement.clientHeight) {
return [document.documentElement.clientWidth, document.documentElement.clientHeight];
} else if (document.body)
return [document.body.clientWidth, document.body.clientHeight];
return [0, 0];
}
document.addEventListener('DOMContentLoaded',
function() {
hwrcss = '#hwr_div_ujs{position:absolute;}.hwr_hidden_ujs{display:none;}.hwr_visible_ujs{display:block;}';
var hwrstyle = document.createElement('style');
hwrstyle.setAttribute('type', 'text/css');
hwrstyle.textContent = hwrcss;
document.getElementsByTagName('head')[0].appendChild(hwrstyle);
hwrdiv = document.createElement('div');
hwrdiv.id = "hwr_div_ujs";
hwrdiv.className = "hwr_hidden_ujs";
document.body.appendChild(hwrdiv);
},
false);
document.documentElement.addEventListener('click',
function(e) {
if (e.ctrlKey !e.shiftKey !e.altKey) {
if (e.target.nodeName == "TEXTAREA" || e.target.type == "text" || e.target.type == "password") {
var tPos = findPos(e.target), wDim = innerDimensions(), sDis = scrollDist();
if (tPos[1] + 293 + e.target.offsetHeight - sDis[1] - wDim[1] 0) {
hwr_div_ujs.style.top = tPos[1] - 293 + "px";
} else {
hwr_div_ujs.style.top = tPos[1] + e.target.offsetHeight + 3 + "px";
}
hwr_div_ujs.style.left = Math.max(0, Math.min(tPos[0], wDim[0] - 430)) + "px";
hwr_div_ujs.className = "hwr_visible_ujs";
penControl();
HWR.load("hwr_div_ujs", e.target, "");
}
} else {
hwr_div_ujs.className = "hwr_hidden_ujs";
}
},
false);
原生JS是自己手写的JS吗?
原生js就是使用javascript来进行一系列的开发,
非原生就是使用了js框架,像jq等就不是原生的,它是js的封装。
Javascript的onblur事件如何手写
document.getElementsById("文本框id").onblur=方法名如:=doit;//方法名不要加();把这个写到onload事件中,
function doit()
{
}
文章标题:手写javascript,手写javascript的call apply bind
网站URL:http://ybzwz.com/article/dsicpgh.html