Skip to content Skip to sidebar Skip to footer

When I Make A Clone Of The Object Shows Me An Error Message

First the following is the code of my own javascript library. (function() { var lib = { elems: [], getElem: function() { var tmpElem = [];

Solution 1:

You create a variable fn which has a reference to "getElem" but since fn is not a property on your lib object then it means that when getElem refers to "this" it will be you global object which is propbably window.

Remove all the following 3 lines

var fn = lib.getElem;
for(var i inlib)
   fn[i] = lib[i];

and then do this

window.$ = function () { return lib.getElem.apply(lib, arguments); };

This will allow getElem to be called as $ but maintaining "lib" as context.

Solution 2:

Although I don't know exactly what you are trying to achieve with those additional lines, just by reading the code, lib.getElem does not have a function called html

lib does.

Hence, just var fn = lib; should do just fine.

Solution 3:

There more ways to achieve this but the root cause is in your getElem() function: return this;

$ is a reference to that function. If you call $() it is called as a function and not as a method. Therefore this refers to window and window has, of course, no html() function.

You could do return lib; to fix the problem.

Post a Comment for "When I Make A Clone Of The Object Shows Me An Error Message"