Solved

jquery 3 and protoype

Posted on 2016-10-07
9
42 Views
Last Modified: 2016-10-18
Dear all,
Once I upgrade to jquery 3.1.1, I got an error when I use prototype:
	Array.prototype.remove = function(from, to) {
	  var rest = this.slice((to || from) + 1 || this.length);
	  this.length = from < 0 ? this.length + from : from;
	  return this.push.apply(this, rest);
	};

Open in new window


PropertiesWindow.js:63 Uncaught TypeError: controller.remove is not a function
0
Comment
Question by:ethar1
  • 4
  • 4
9 Comments
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41834700
That error does not look like it pertains to the snippet you have posted.
0
 

Author Comment

by:ethar1
ID: 41834716
Yes its for :
controller = new CategoryController(orignalData);
controller.init(options);
function CategoryController(dataArray) {
    if (dataArray) this.dataArray = dataArray;
    this.category = {};
    this.listObjectMap = {}; // each category with it's List.js object
}

CategoryController.prototype = {
    curGroupBy: "groupId",
    curSortBy: "asc",
    dataArray: [],
    categories: {},
    init: function (options) {
        this.groupBy(this.curGroupBy);
        this.render(options);
        
		var self = this; 
    }
};

Open in new window

0
 

Author Comment

by:ethar1
ID: 41834747
Error raised :
PropertiesWindow.js:63 Uncaught TypeError: controller.init is not a function
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41834755
This works for me
<script src="http://code.jquery.com/jquery-3.1.1.js"></script>
<script>
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};
var x = new Array();
x.push(1);
x.push(2);
x.push(3);
x.push(4);
x.push(5);
x.push(6);
x.push(7);
x.push(8);
console.log(x);
x.remove(3,6);
console.log(x);
</script>

Open in new window

0
 

Author Comment

by:ethar1
ID: 41834928
what about the code I sent?
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41834949
I am guessing it is an ordering issue - what happens if you try this
function CategoryController(dataArray) {
    if (dataArray) this.dataArray = dataArray;
    this.category = {};
    this.listObjectMap = {}; // each category with it's List.js object
}

CategoryController.prototype = {
    curGroupBy: "groupId",
    curSortBy: "asc",
    dataArray: [],
    categories: {},
    init: function (options) {
        this.groupBy(this.curGroupBy);
        this.render(options);
        
		var self = this; 
    }
};
controller = new CategoryController(orignalData);
controller.init(options);

Open in new window

It seems to work for me bar the errors about groupBy() not being defined.
0
 

Author Comment

by:ethar1
ID: 41834959
Thanks for your reply,
groupBy is already defined , I just remove it to make code simple.

In face  I call
controller = new CategoryController(orignalData);
controller.init(options);

Open in new window

from onclick function.

Also The error raised only when I change jquery from 2.1.1 to 3.1.1.
0
 
LVL 54

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41834996
In my sample above I am running JQuery 3.1.1 - I don't get the error you are getting. The only error I get is the groupBy is missing. If I add those in - no issues. So I am guessing there is something else happening.

My working code
<script src="http://code.jquery.com/jquery-3.1.1.js"></script>
<script>
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};
var x = new Array();
x.push(1);
x.push(2);
x.push(3);
x.push(4);
x.push(5);
x.push(6);
x.push(7);
x.push(8);
console.log(x);
x.remove(3,6);
console.log(x);
function CategoryController(dataArray) {
    if (dataArray) this.dataArray = dataArray;
    this.category = {};
    this.listObjectMap = {}; // each category with it's List.js object
}

CategoryController.prototype = {
    curGroupBy: "groupId",
    curSortBy: "asc",
    dataArray: [],
    categories: {},
	groupBy: function(gb) {
		console.log(gb);
	},
	render: function(options) {
		console.log(options);
	},
    init: function (options) {
        this.groupBy(this.curGroupBy);
        this.render(options);
        
		var self = this; 
    }
};
controller = new CategoryController(x);
var options = {};
controller.init(options);
</script>

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question