<script> function individual(msg) // class individual { this.mx = msg; // mx is a public property of class individual instance var zuper = this.mx + '!'; // zuper is a private property of class individual instance this.show = function() { // public method alert(zuper); } this.showSomething = function() { // public method alert(this.something); quick(); } function quick() { // private method alert('quick'); } } var x = new individual("Hello"); var y = new individual("World"); alert(x.mx); // shows Hello alert(y.mx); // shows World alert(x.zuper); // shows undefined y.zuper = "xxx"; // but this one creates a different instance of outside zuper on y's instance, but this is different from y's inside zuper alert(y.zuper); // shows xxx x.show(); // shows "Hello!" y.show(); // to prove that the outside zuper instance on y is different from inside(acts like a private property), this will not show "xxx", this shows "World!" y.something = "George"; // you can attach properties on javascript's object on whim y.showSomething(); // shows "George" y.quick(); // this will make javascript terminate silently alert("Not shown"); // won't get executed </script>
"Simplicity can't be bought later, it must be earned from the start" -- DB
Showing posts with label NotEnoughJquery. Show all posts
Showing posts with label NotEnoughJquery. Show all posts
Wednesday, April 27, 2011
Javascript object-based programming for C# object-oriented programmers
Sunday, April 24, 2011
Put prefix to a property
<script> String.prototype.splice = function( idx, rem, s ) { // sourced from: http://stackoverflow.com/questions/4313841/javascript-how-can-i-insert-a-string-at-a-specific-index return (this.slice(0,idx) + s + this.slice(idx + Math.abs(rem))); }; String.prototype.putPrefixToProperty = function(prefix) { ndx = -1; for(i = this.length - 1; i > 0; --i) { if (this[i] == '.') { ndx = i; break; } } return this.splice(++ndx,0,prefix) } alert("ProductId".putPrefixToProperty("txt_")); alert("PurchasedDto.ProductId".putPrefixToProperty("txt_")); alert("PurchasedDto.LineItem.ProductId".putPrefixToProperty("txt_")); </script>
Output:
txt_ProductId PurchasedDto.txt_ProductId PurchasedDto.LineItem.txt_ProductId
Trivia: Did you know you can copy alert's text(or any dialog for that matter) by pressing Ctrl+C
Subscribe to:
Posts (Atom)