Skip to content

Instantly share code, notes, and snippets.

@jixunmoe
Created October 9, 2014 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jixunmoe/5963f6c90cf1fad28a6d to your computer and use it in GitHub Desktop.
Save jixunmoe/5963f6c90cf1fad28a6d to your computer and use it in GitHub Desktop.
网易音乐 NEJ 调试分析
// 网易音乐 NEJ 内核
(function() {
window.NEJ = window.NEJ || {};
NEJ.O = {};
NEJ.R = [];
NEJ.F = NEJ.emptyFunction = function() {
return false;
};
// NEJ.P('a.b.c') => window.a.b.c = {}
NEJ.getWindowScope = NEJ.P = function(targetScope) {
if (!targetScope || !targetScope.length)
return null;
var scope = window;
var names = targetScope.split(".");
var scopeLen = names.length;
var beginIndex = names[0] == "window" ? 1 : 0;
for (var i = beginIndex; i < scopeLen; i++) {
scope = scope[names[i]] = scope[names[i]] || {};
}
return scope
};
NEJ.getScope = NEJ.Q = function(scope, targetScope) {
scope = scope || NEJ.O;
var names = targetScope.split(".");
var nameLen = names.length;
for (var i = 0; i < nameLen; i++) {
scope = scope[names[i]];
// 不明?
if (!scope) break;
}
return scope
};
// 注册模组
NEJ.registerModule = NEJ.C = function() {
var notFunction = function () {
// [].toString.call (function () {})
// => "[object Function]"
// (function () {}).toString
// Will return the function it self.
return NEJ.O.toString.call(arguments[0]) != "[object Function]"
};
/**
* Find key name inside an object.
*
* @param {Any} val The value to be searched.
* @param {Object} obj The object to search.
*/
var findKeyInObject = function(val, obj) {
for (var x in obj)
if (val == obj[x])
return x;
return null
};
var mapTable1 = {
dJ: 0,
bO: 1,
cP: 2,
cA: 3,
cS: 4,
uQ: 5,
wG: 6,
gK: 7
},
mapTable2 = {
dY: 0,
bS: 1,
dm: 2,
dh: 3,
dq: 4,
JH: 5,
Ky: 6,
DN: 7
};
return function () {
var ClassBase = function() {
this._preInit ();
return this._init.apply (this, arguments);
};
ClassBase.prototype._preInit = ClassBase.prototype.zP = NEJ.emptyFunction;
ClassBase.prototype._init = ClassBase.prototype.dJ = NEJ.emptyFunction;
ClassBase.br = ClassBase.init = function(parentClass, doMergeParent) {
if (notFunction(parentClass))
return;
if (doMergeParent == null || doMergeParent)
NEJ.mergeWithCallback (this, parentClass, notFunction);
this.parent = this.Ij = parentClass;
this.parentPrototype = this.cE = parentClass.prototype;
// 从父类继承函数
var tmpGetProto = function() {};
tmpGetProto.prototype = parentPrototype;
this.prototype = new tmpGetProto ();
var classPrototype = this.prototype;
classPrototype.constructor = this;
var targetKeyName;
// x => dJ, bO, cP, cA, cS, uQ, wG, gK
for (var x in mapTable1) {
targetKeyName = findKeyInObject(mapTable1[x], mapTable2);
// 如果映射列表没有或目标不存在则跳过
if (!targetKeyName || !this.cE[x])
continue;
// 否则,添加这个进来。
classPrototype[x] = function(bo) {
return function() {
this[bo].apply(this, arguments)
}
}(targetKeyName)
}
var sS = {};
for (var x in mapTable2) {
targetKeyName = findKeyInObject(mapTable2[x], mapTable1);
if (!targetKeyName || !this.cE[targetKeyName]) continue;
sS[targetKeyName] = parentClass;
classPrototype[x] = function(bo) {
return function() {
var bhc, bX = this.zw[bo],
zR = bX.prototype[bo];
this.zw[bo] = bX.Ij || parentClass;
if (!!zR) bhc = zR.apply(this, arguments);
this.zw[bo] = parentClass;
return bhc
}
}(targetKeyName)
}
classPrototype.zP = function() {
this.zw = NEJ.mergeWithCallback({}, sS)
};
classPrototype.SQ = classPrototype.dY;
// 交由模组进行后续填充
return classPrototype
};
return ClassBase
}
}();
NEJ.X = NEJ.mergeWithCallback = function(objSource, objExtend, callback) {
if (!objSource || !objExtend)
return objSource;
callback = callback || NEJ.emptyFunction;
for (var x in objExtend)
if (objExtend.hasOwnProperty(x) && !callback(objExtend[x], x))
objSource[x] = objExtend[x];
return objSource;
};
NEJ.EX = NEJ.mergeObject = function(objSource, objExtend) {
if (!objSource || !objExtend)
return objSource;
for (var x in objSource)
if (objSource.hasOwnProperty(x) && objExtend[x] != null)
objSource[x] = objExtend[x]
return objSource
};
// 还没搞懂什么用
Function.prototype.ce = function(wx, xH) {
var f = NEJ.emptyFunction,
xH = xH || f,
wx = wx || f,
foo = this
return function() {
var objCheck = {
args: NEJ.R.slice.call(arguments, 0)
};
wx(objCheck);
if (!objCheck.stopped) {
objCheck.value = foo.apply(this, objCheck.args);
xH(objCheck)
}
return objCheck.value
}
};
// Function::bind
Function.prototype.bha = function() {
var args = arguments,
scope = arguments[0],
that = this;
return function() {
var newArgs = NEJ.R.slice.call(args, 1);
NEJ.R.push.apply(newArgs, arguments);
return that.apply(scope || window, newArgs)
}
};
Function.prototype.hP = function() {
var Tb = arguments,
dv = NEJ.R.shift.call(Tb),
uF = this;
return function() {
NEJ.R.push.apply(arguments, Tb);
return uF.apply(dv || window, arguments)
}
};
if (!String.prototype.trim) {
String.prototype.trim = function() {
var cG = /(?:^\s+)|(?:\s+$)/g;
return function() {
return this.replace(cG, "")
}
}()
}
if (!window.MWF) window.MWF = NEJ;
if (!window.mwf) window.mwf = NEJ.P("nej");
// 避免出错
if (!window.console) {
NEJ.P("console").log = NEJ.emptyFunction;
NEJ.P("console").error = NEJ.emptyFunction
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment