首页 > 编程语言 >深入学习jquery源码之map()

深入学习jquery源码之map()

时间:2023-02-23 22:08:14浏览次数:40  
标签:jquery map return function window length 源码 jQuery


概述

将一组元素转换成其他数组(不论是否是元素数组)

你可以用这个函数来建立一个列表,不论是值、属性还是CSS样式,或者其他特别形式。这都可以用'$.map()'来方便的建立。

参数

callback,给每个元素执行的函数

把form中的每个input元素的值建立一个列表。

<p><b>Values: </b></p>
<form>
<input type="text" name="name" value="John"/>
<input type="text" name="password" value="password"/>
<input type="text" name="url" value="http://ejohn.org/"/>
</form>
$("p").append( $("input").map(function(){
return $(this).val();
}).get().join(", ") );

结果

[ <p>John, password, http://ejohn.org/</p> ]

由于返回值是 jQuery 封装的数组,使用 get() 来处理返回的对象以得到基础的数组。

.map() 方法对于获得或设置元素集的值特别有用。

<form method="post" action="">
<fieldset>
<div>
<label for="two">2</label>
<input type="checkbox" value="2" id="two" name="number[]">
</div>
<div>
<label for="four">4</label>
<input type="checkbox" value="4" id="four" name="number[]">
</div>
<div>
<label for="six">6</label>
<input type="checkbox" value="6" id="six" name="number[]">
</div>
<div>
<label for="eight">8</label>
<input type="checkbox" value="8" id="eight" name="number[]">
</div>
</fieldset>
</form>

能够获得复选框 ID 组成的逗号分隔的列表

$(':checkbox').map(function() {
return this.id;
}).get().join(',');
字符串:"two,four,six,eight"

map也可以遍历数组, 用法与 forEach 相似,实际效率还比不上foreach:

arr.map(function(value,index,array){

  //code

})

 

 

 

jquery的map()源码

(function (global, factory) {

if (typeof module === "object" && typeof module.exports === "object") {

module.exports = global.document ?
factory(global, true) :
function (w) {
if (!w.document) {
throw new Error("jQuery requires a window with a document");
}
return factory(w);
};
} else {
factory(global);
}

// Pass this if window is not defined yet
}(typeof window !== "undefined" ? window : this, function (window, noGlobal) {

var version = "1.11.3",
jQuery = function (selector, context) {
return new jQuery.fn.init(selector, context);
},

// Support: Android<4.1, IE<9
// Make sure we trim BOM and NBSP
rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,

// Matches dashed string for camelizing
rmsPrefix = /^-ms-/,
rdashAlpha = /-([\da-z])/gi,

// Used by jQuery.camelCase as callback to replace()
fcamelCase = function (all, letter) {
return letter.toUpperCase();
};

jQuery.fn = jQuery.prototype = {
// The current version of jQuery being used
jquery: version,

constructor: jQuery,

// Start with an empty selector
selector: "",

// The default length of a jQuery object is 0
length: 0,
// Take an array of elements and push it onto the stack
// (returning the new matched element set)
pushStack: function (elems) {

// Build a new jQuery matched element set
var ret = jQuery.merge(this.constructor(), elems);

// Add the old object onto the stack (as a reference)
ret.prevObject = this;
ret.context = this.context;

// Return the newly-formed element set
return ret;
},
map: function (callback) {
return this.pushStack(jQuery.map(this, function (elem, i) {
return callback.call(elem, i, elem);
}));
}
}:

jQuery.extend({
// arg is for internal usage only
map: function (elems, callback, arg) {
var value,
i = 0,
length = elems.length,
isArray = isArraylike(elems),
ret = [];

// Go through the array, translating each of the items to their new values
if (isArray) {
for (; i < length; i++) {
value = callback(elems[i], i, arg);

if (value != null) {
ret.push(value);
}
}

// Go through every key on the object,
} else {
for (i in elems) {
value = callback(elems[i], i, arg);

if (value != null) {
ret.push(value);
}
}
}

// Flatten any nested arrays
return concat.apply([], ret);
}
});

function isArraylike(obj) {

// Support: iOS 8.2 (not reproducible in simulator)
// `in` check used to prevent JIT error (gh-2145)
// hasOwn isn't used here due to false negatives
// regarding Nodelist length in IE
var length = "length" in obj && obj.length,
type = jQuery.type(obj);

if (type === "function" || jQuery.isWindow(obj)) {
return false;
}

if (obj.nodeType === 1 && length) {
return true;
}

return type === "array" || length === 0 ||
typeof length === "number" && length > 0 && (length - 1) in obj;
}


jQuery.noConflict = function (deep) {
if (window.$ === jQuery) {
window.$ = _$;
}

if (deep && window.jQuery === jQuery) {
window.jQuery = _jQuery;
}

return jQuery;
};

if (typeof noGlobal === strundefined) {
window.jQuery = window.$ = jQuery;
}

return jQuery;

}));

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

标签:jquery,map,return,function,window,length,源码,jQuery
From: https://blog.51cto.com/u_11837698/6081921

相关文章

  • 深入学习jquery源码之插件机制(二)
    高级插件概念链接提供对默认插件设置的公共访问我们可以而且应该对上面的代码做出的改进并公开默认的插件设置。这很重要,因为它使插件用户很容易用最少的代码覆盖/自定义......
  • 深入学习jquery源码之jQuery的二次开发
    深入学习jquery源码之jQuery的二次开发jquery.js的设计与实现(function(global,factory){if(typeofmodule==="object"&&typeofmodule.exports==="object")......
  • 深入学习jquery源码之merge()和unique()
    深入学习jquery源码之merge()概述:合并两个数组到第一个数组上。返回的结果会修改第一个数组的内容——第一个数组的元素后面跟着第二个数组的元素。要去除重复项,请使用$.uni......
  • 深入学习jquery源码之show()和hide()
    jQueryshow([speed,[easing],[fn]])概述:显示隐藏的匹配元素。这个就是'show(speed,[callback])'无动画的版本。如果选择的元素是可见的,这个方法将不会改变任何东西。......
  • 深入学习jquery源码之继承框架的实现
    深入学习jquery源码之继承框架的实现继承框架的实现实现自己的extend方法/*SimpleJavaScriptInheritance*ByJohnResighttp://ejohn.org/*MITLicensed.*///Insp......
  • 深入学习jquery源码之创建科学、复用率高的对象
    常规创建对象的方式通过{},[]来定义数组和对象1.{}大括号,表示定义一个对象,大部分情况下要有成对的属性和值,或是函数。2.[]中括号,表示一个数组,也可以理解为一个数组对象......
  • 深入学习jquery源码之原型链
    深入学习jquery源码之原型链prototype原型,对该函数对象的对象原型的引用,是函数对象的默认属性我们在定义函数的时候,函数定义的时候函数本身就会默认有一个prototype的属性,而......
  • 深入学习jquery源码之继承方案的选择
    prototype实现继承原型链继承的主要思想是:让子类型的引用指向父类型的实例。每次在函数(类)中定义了成员方法,都会导致实例有副本,因此可以借助prototype原型,进行改进先访问自己......
  • 深入学习jquery源码之extend()
    jQuery.extend([deep],target,object1,[objectN])概述:用一个或多个其他对象来扩展一个对象,返回被扩展的对象。如果不指定target,则给jQuery命名空间本身进行扩展。这有助......
  • 深入学习jquery源码之上传附件插件的实现
    深入学习jquery源码之上传附件插件的实现/***上传附件通用JS(基于layerUI)*/;(function($){vardefaults={url:"/attach/upload",fieldCode:"ab......