angularjs - Can't access to methods that added via Prototype to Javascript object -


i have object declaration inside angular.js module:

    $scope.test=function(){      };      $scope.test.prototype.push = function(data) {         return data;     }; 

and call this:

var = $scope.test.push(1); console.error(a); 

but error:

error: undefined not function (evaluating '$scope.test.push(1)') 

why cant access methods added via prototype object?

you seem mistaking function's prototype property internal prototype of object.

a relevant quote book eloquent javascript:

it important note distinction between way prototype associated constructor (through prototype property) , way objects have prototype (which can retrieved object.getprototypeof). actual prototype of constructor function.prototype since constructors functions. prototype property prototype of instances created through not own prototype.

what means in context of code sample:

$scope.test.prototype.push = function(data) {     return data; }; 

here you've added push function prototype property of $scope.test function, present on prototype of objects created using test function constructor function (with new keyword).

$scope.test, however, remains empty function, has no .push() method, resulting in error. if want add push method every function, use function.prototype (note function constructor new functions created), i'm not sure going this.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -