<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用对象方法实现数组去重</title> </head> <body> <script> Array.prototype.unique = function() { var newArr = []; for(var i = 0;i<this.length;i++) { if(newArr.indexOf(this[i]) == -1) { newArr.push(this[i]); } } return newArr; }; var arr = [1,3,1,2]; console.log(arr.unique()); </script> </body> </html>