Search the Community
Showing results for tags 'prototype'.
-
/* Merry Christmas everyone! */ I have an array of objects: arr = [ obj1, obj2 ]; Each object has prototypes: obj1.txt = "az"; obj2.txt = "abc"; obj1.isRoot = false; obj2.isRoot = false; And there's an array called dictionary, it formed like this: var dictionary = [ ["a","(n)"], ["ab","(n)"], ["abc","(n)"], ["abcd","(n)"] ] What I try to achieve is when obj.txt is similar to dictionary[x][0], then obj.isRoot turn to TRUE. I've made the function below so far but it doesn't work. function go(){ for ( i=0; i<arr.length; i++ ){ txt_source = arr[i].txt.replace( /\~|\`|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\_|\-|\+|\=|\{|\}|\[|\]|\:|\;|\"|\'|<|\>|\,|\.|\?|\/|\\|(0-9)/g, "" ); ori = txt_source.toLowerCase(); regex = new RegExp( '\\b' + ori, 'gi'); for ( m=0; m<dictionary.length; m++ ){ if ( dictionary[m][0].match(regex) ){ arr[i].isRoot = true; // I want this happen for obj2.txt = "abc". } else { arr[i].isRoot = false; } } } console.log( ahoc_arr[x].isRoot ); // all objects always FALSE } Please help! Thanks!
-
Hi, I'm studying a constructor function pattern. I have made this constructor: function Person( firstname, age, place ) { this.firstname = firstname; this.age = age; this.greeting = { Hello: function(){ console.log( "Hello " + firstname ); }, YouAre: function(){ console.log( "You are " + age + " years old"); } } this.greeting2 = { Hello: function(){ console.log( "Hello " + this.firstname ); }, YouAre: function(){ console.log( "You are " + this.age ); } } } Person.prototype = { greeting3: { Hello: function(){ console.log( "Hello " + this.firstname ); }, YouAre: function(){ console.log( "You are " + this.age ); } } } var Clara = new Person( 'Clara', '20' ); Clara.greeting.Hello(); // Hello Clara Clara.greeting.YouAre(); // You are 20 years old Clara.greeting2.Hello(); // Hello undefined Clara.greeting2.YouAre(); // You are undefined Clara.greeting3.Hello(); // Hello undefined Clara.greeting3.YouAre(); // You are undefined On the greeting2.Hello and greeting2.YouAre methods, 'this' name and age result is undefined, the same with greeting3 method. The question is: How do we get a property value through a method inside an object array? and who is 'this' in greeting2 and greeting3 functions? Thanks!
- 11 replies
-
- constructor
- prototype
-
(and 1 more)
Tagged with: