seen from Germany

seen from United States

seen from United States
seen from United States
seen from United States

seen from Indonesia
seen from Russia
seen from Brazil
seen from United States
seen from United States

seen from United States
seen from United States

seen from Kazakhstan
seen from United States

seen from United States

seen from Canada
seen from Australia
seen from Italy
seen from China
seen from United States

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch โข No registration required โข HD streaming
W.B. Yeats
โROSE of all Roses, Rose of all the World! The tall thought-woven sails, that flap unfurled Above the tide of hours, trouble the air, And Godโs bell buoyed to be the waterโs care; While hushed from fear, or loud with hope, a band With blown, spray-dabbled hair gather at hand. Turn if you may from battles never done, I call, as they go by me one by one, Danger no refuge holds, and war no peace, For him who hears love sing and never cease, Beside her clean-swept hearth, her quiet shade: But gather all for whom no love hath made A woven silence, or but came to cast A song into the air, and singing past To smile on the pale dawn; and gather you Who have sought more than is in rain or dew Or in the sun and moon, or on the earth, Or sighs amid the wandering starry mirth, Or comes in laughter from the seaโs sad lips; And wage Godโs battles in the long grey ships. The sad, the lonely, the insatiable, To these Old Night shall all her mystery tell; Godโs bell has claimed them by the little cry Of their sad hearts, that may not live nor die.
Rose of all Roses, Rose of all the World! You, too, have come where the dim tides are hurled Upon the wharves of sorrow, and heard ring The bell that calls us on; the sweet far thing. Beauty grown sad with its eternity Made you of us, and of the dim grey sea. Our long ships loose thought-woven sails and wait, For God has bid them share an equal fate; And when at last defeated in His wars, They have gone down under the same white stars, We shall no longer hear the little cry Of our sad hearts, that may not live nor die.
The Sweet Far Thingโ โ W.B. Yeats
javascript object
http://nefariousdesigns.co.uk/object-oriented-javascript.htmlย ์์ ์ฐธ์กฐ
Object-Oriented Javascript
Posted Wednesday 24thย May, 2006
Update October 2010:ย Iโm currently following up this article with some notes about development method, and then again with a more in-depth look at current practices for object-oriented JavaScript. Hopefully, this should serve to correct some of my errors in this older article. Once youโve finished reading this article, please read the following:
Object-Oriented JavaScript Follow-Up Part 1: Method
Object-Oriented JavaScript Follow-Up Part 2: Technical
Following on from my earlier post, โObject-Oriented Concepts,โ itโs time we started to have a look at some examples of execution. Iโm going to start with Javascript because I believe this to have widest appeal - PHP, as a server-side language, is probably of interest to fewer developers so Iโll cover it later.
So without further ado, hereโs how to objectify your javascriptโฆ
Objects in Javascript
Although Javascript shouldnโt beย classedย as an object-oriented language, pretty much everything within it is objectย based; from DOM scripting (Documentย Objectย Model) through to specific built-in objects such as Image and Date. This basically means that we can adoptย someย OOPย concepts but not all.
Javascript handles objects in a number of ways; a developer can define an object and then instantiate with theย newย operator, a developer can declare an object on the fly using theย object literalย or a developer can extend an existing object (either built-in or user-defined) using itsย prototype.
In fact, even data-types that can be declared literally, such as arrays and strings, can also be declared as objects. This is mainly so that object methods can be applied to literal values when needed. Javascript does this by temporarily converting your literal into an object. A good example would be theย String.lengthmethod.
Sounding complicated? Donโt worry, itโs really very easy. Letโs start getting our hands dirtyโฆ
The Object Literal
In programmer-speak, a โliteralโ is any value declaredย literally. Good examples would be string literals, array literals and boolean literals - the literal is the partย afterย the โ=โ assignment operator:
// String literal var my_string = "2468 This is a string"; // Array literal var my_array = ['element1', 'element2', 'elephant']; // Boolean literal var my_boolean = true;
As a developer, you probably use these methods day-in, day-out; it still amazes me, however, how many programmers I speak to that donโt know the correct terminology!
In Javascript, we can also declareย objectsย as a literal:
function getArea(radius) { // return the radius of our circle using the // PI attribute of the built-in Math object return (radius * radius) * Math.PI; } var circle = { radius : 9, getArea : getArea(this.radius) };
Here weโve defined an object,ย circle, with a radius of 9. If we wanted to obtain the area of our circle, we could use the following line of code:
var my_radius = circle.getArea();
Fantastic! Weโve got ourselves a circle; but what happens when we want to declareย moreย circles, all with varying radii?
Imagine we want to create lots ofย circleย objects using our object as a base - if weโve declared using the object literal, weโre not able to do that! To a certain extent, thatโs the difference between objects andย classes(seeย my earlier post); if we want our object to behave more like a class (which, unfortunately, arenโt really supported in Javascript even though we can mimic the behaviour), we need to use objects that utilise theย newย operator.
Note:ย For more information on the object literal, I recommend reading Chris Heilmannโs โShow Love to The Object Literal,โ and Dustin Diazโs โJSON for the Masses.โ
Theย newย Operator
Theย newย operator creates an instance of any built-in or user-defined object - basically allowing us to re-use a user-defined obect (much like the behaviour of a class). Here are a couple of examples usingย built-inobjects:
// Create a date object var obj_today = new Date(); // Preload an image using the Image object var obj_supper = new Image(); obj_supper.src = 'thelastsupper.jpg';
Take note that Javascriptโs built-in objects do not always use a proper class interface - ie. you can set attributes without using a method. In most OOP languages this is considered bad practice and you can often prevent it with the use of access modifiers (public, private, protected). Even though Javascript lets us get away with this method, when using its built-in objects, I personally tend to write a proper interface forย my ownย objects.
Ok, so thatโs how you declare using the built-in objects, but whatย aboutย something user-defined?
When using theย newย keyword to declare user-defined objects, the objects require a constructor. Object structure (including an objectโs constructor) is handled somewhat differently to most languages in Javascript. Hereโs the syntax for defining this type of object:
function circle(radius) { this.radius = radius; this.getArea = getArea; // Don't forget that a constructor should // always retain a valid state - so it should // always return true return true; } function getArea() { // return the radius of our circle using the // PI attribute of the built-in Math object return (this.radius * this.radius) * Math.PI; }
In the example above, youโll notice that the syntax is almost completely identical to declaring a function. This function is the constructor of our object and attributes and methods are declared within it, using theย thisย keyword.
Notice the Math object doesnโt need to be instantiated before we can use it - this is simply because itโs special and is always available for use.
We can now instantiate our object using theย newย operator as before:
var obj_pizza = new circle(9);
And we can work out the area of our circle using the following line of code:
var flt_area = obj_pizza.getArea();
Encapsulation
There is one problem with the above definitions of an object, however, and thatโs the lack ofย encapsulation. If we were to include another piece of code that used aย getArea()ย function (for instance aย triangleย object), ourย circleย objectโsย getArea()ย method would be over-written. To better encapsulate our methods, we can define our objects either literally like so:
var circle = { radius : 9, getArea : function() { return (this.radius * this.radius) * Math.PI; } };
Or non-literally like so:
function circle(radius) { this.radius = radius; this.getArea = function() { return (this.radius * this.radius) * Math.PI; }; return true; }
In both cases this limits the scope of eachย getArea()ย method to eachย circleย object.
Inheritance andย prototype
Theย prototypeย property is a useful feature in Javascript - it basically allows us to add attributes or methods to an object. This is a form ofย inheritance.
As a quick example ofย prototypeย in use, letโs add a function to work out circumference:
function circle(radius) { this.radius = radius; this.getArea = function() { return (this.radius * this.radius) * Math.PI; }; return true; } // Add our new method circle.prototype.getCircumference = function() { return this.radius * Math.PI * 2; } // Instantiate our object var my_pizza = new circle(9); // Call our new method var flt_pizza_circ = my_pizza.getCircumference();
โBut wait, โ I hear you cry, โwhat if we want to inherit an entire object?โ
Not a problem,ย prototypeย can handle this like so:
// Declare two objects - we're going to want Lion to // inherit from cat function cat() { this.eyes = 2; this.legs = 4; this.diet = 'carnivore'; return true; } function lion() { this.mane = true; this.origin = 'Africa'; return true; } // Now comes the inheritance lion.prototype = new cat(); // We can now obtain lion.diet var simba = new lion(); var simba_diet = simba.diet;
Ok, so now that we understand inheritance (youย doย understand, right?), letโs take a lookย composition.
Composition
Association
Association, in Javascript, is pretty straight forward. Hereโs an example:
// Define an object function brick() { return true; } // Define an object function wall() { this.brick1 = new brick(); this.brick2 = new brick(); this.brick3 = new brick(); return true; }
Just like the pseudo-code example in my earlier post, we can see that the object โwallโ now contains three instances of the โbrickโ object. This isย associationย - the wall object โhasโ three bricks.
Now letโs take a look at aggregation.
Aggregation
Aggregation, as a relationship, is reliant on the ability to pass object and function parametersย by reference. This basically means that a parameter represents aย pointerย to the actual object passed and not just a copy. Thankfully, Javascript handles all parameters in this way - which makes our aggregation relationship nice and easy:
function person { return true; } function car(driver) { this.driver = driver; return true; } var me = new person(); var myMotor = new car(me);
This relationship allows us to โuseโ the โmeโ object within our โmyMotorโ object. We could also use the โmeโ object in any other objects that require it - and weโd only need that single instantiation. This is immensely helpful when weโre using objects that control behaviours such as XmlHttpRequest.
Update:ย Thanks toย Jonathan Snook, Iโve recently discovered that Javascript doesnโt passย allย parameters by reference - in fact working out how Javascript has handled your parameter can be quite confusing. In our particular case, the parameter is passed by referenceย because it is an object. When weโre not working with objects, this might not be the case. For more detailed information, please take a look at Jonathanโs excellent article, โJavascript: Passing by Value or by Reference.โ
Update 2:ย Stefan Van Reethโs fantastic comment, below, looks at references in a little more detail. I definitely recommend reading it as his explanation and examples may clear up confusion.
Polymorphism
Due to its object-based nature, Javascript handles polymorphism very well. Take a look at the following example:
function Person() { //... this.Speak = function() { // ... } } function Employee() { // ... this.Speak = function() { // ... } } AND (!!!) Employee.prototype = new Person(); // inheritance user emp = new Employee(); emp.Speak();
Employee inherits from Person and, as a result, already has a Speak() method. Because we want the Speak() method in Employee to do something different, we overload it with a new method definition. This is polymorphism in action.
Update:ย My original example for Polymorphism was entirely incorrect. Thanks go toย โJoe is all you need to knowโย for pointing out my error and supplying this correct example.
Summary
So thatโs a brief guide to adoptingย OOPย practices in Javascript; I think youโll agree that it adds real power when dealing with complicated scripts. However, that raises a common issue withย OOPย - you shouldnโt just use it for the sake of it. In fact, I often find that developers whoย areย using these practices for everything are often the same developers who donโt understand the concepts fully.
My nextย OOPย post will be regarding object-oriented PHP but, after an inquisitive email fromย Nate Logan, I also intend writing a piece aboutย abstractionย - the practice of reducing a process down to itโs root concepts and modelling them in the programming language of your choice.
javascript ๊ฐ์ฒด
http://www.clearboth.org/45_objects_in_javascript/ย ์์ ์ฐธ์กฐ
์๊ฐ
์ด์ ๊ธย ์์ ํจ์์ ์ปจ์ ์ ๋ํด ์๊ฐํ๋ค. ํจ์๋ฅผ ์ฌ์ฉํจ์ผ๋ก์ ํ๋ก๊ทธ๋จ๋ค์ ๋ ผ๋ฆฌ์ ์ธ ๋ฉ์ด๋ฆฌ๋ค๋ก ์ชผ๊ฐ์ ๋น์ ์ ์ฝ๋๋ฅผ ์ข๋ ์ ์กฐ์งํํ๊ณ , ์ฝ๊ฒ ์ฌ์ฌ์ฉํ ์ ์์๋ค. ์ด์ ๋น์ ์ด ์๋ฐ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋๋ฐ์ ํต์ฌ์ ์ธ ๊ฐ๋ ๋ค์ ๊ฑฐ๋ถ๊ฐ ์์ด ๋ฐ์๋ค์ผ ์ ์๊ฒ ๋์์ผ๋ฏ๋ก, ๊ฐ์ฒด ์ ๋ํด ์๊ฐํจ์ผ๋ก์ ํ์ฉ์ ํญ์ ๋์ฑ ๋ํ๋ณด๊ณ ์ ํ๋ค. ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํจ์ผ๋ก์ ํจ์๋ก ์ ์ํ ๊ธฐ๋ฅ์ฑ๋ค์ ํ๋๋ก ๋ฌถ์ ์ ์๊ณ , ๋ํ ๊ทธ๊ฒ์ ์ด๊ณณ์ ๊ณณ์ผ๋ก ์ ๋ฌํ๋ฉฐ ๋ํ ์ฐธ์กฐํ ์ ์๋ค. ์ด๋ฌํ ๋ฅ๋ ฅ์ ๋น์ ์ด ์์ผ๋ก ์์ฑํ ์ฝ๋์ ์๋นํ ์ค์ง์ ์ธ ์๋ฏธ๋ฅผ ๊ฐ๊ฒ ๋ ๊ฒ์ด๋ค โ ๋น๋ก ์ง๊ธ ์ด ์๊ฐ์๋ ๋ค์ ๋ชจํธํ๊ฒ ๋ค๋ฆฌ๊ฒ ์ง๋ง ๋ง์ด๋ค.
๋์น์ฑ์ง ๋ชปํ์์๋ ์์ง๋ง, ๋น์ ์ ์ด ์๋ฆฌ์ฆ๋ฅผ ๋ฐ๋ผ ์ค๋ฉด์ ์๋ฌต์ ์ผ๋ก ๊ฐ์ฒด๋ค์ ์ฌ์ฉํด ์๋ค. ์ด์ ์ด๊ฒ์ ๋ํด ์ข ๋ ๋ช ์์ ์ธ ์ค๋ช ์ ํจ์ผ๋ก์ ์๋ฐ์คํฌ๋ฆฝํธ์์ ๊ฐ์ฒด๊ฐ ์ด๋ป๊ฒ ๋์ํ๋์ง, ๊ทธ๋ฆฌ๊ณ ๊ฐ์ฒด๋ฅผ ํตํด ๋น์ ์ ํ๋ก๊ทธ๋จ์ ์ข ๋ ๋ช ํํ๊ฒ ํ๊ณ ์ฌ์ฌ์ฉํ๊ธฐ ์ฝ๋๋ก ํ๋ ๋ฐฉ๋ฒ์ ์๋ ค์ฃผ๊ฒ ๋ค.
์ด ๊ธ์ ๊ตฌ์กฐ๋ ๋ค์๊ณผ ๊ฐ๋ค:
์ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋๊ฐ?
์ต์ํ ์์ญ
๊ฐ์ฒด ์์ฑํ๊ธฐ
์๊ฐ ์ฐธ์กฐ
์ฐ๊ด ๋ฐฐ์ด๋ก์์ ๊ฐ์ฒด
๊ฐ์ฒด ๋ฆฌํฐ๋ด
์์ฝ โ ๋ฐฐ์ธ๊ฒ์ด ๋ ๋ง์ด ์๋ค
์ฝ์ด๋ณผ๊ฒ๋ค
์ฐ์ต๋ฌธ์
๋ค์ด๋ก๋ํ๊ฑฐ๋ ์ง์ ์คํํด๋ณผ ์ ์๋ ์์ ๊ฐ ์ค๋น๋์ด ์๋ค. ์ด๊ฒ์ ์ผ๊ฐํ์ ๋ฉด์ ์ ๊ณ์ฐํ๋ ์ฝ๋๋ฅผ ํฌํจํ๊ณ ์๋๋ฐ, ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ ๊ฒ๊ณผ ์ฌ์ฉํ์ง ์์ ๊ฒ์ด ์๋ค. ์ด ์ฝ๋๋ ์๋์์ ์ค๋ช ํ๋ ์์ ๋ค๋ก ๋ง๋ค์ด์ ธ ์๋ค.ย ์ผ๊ฐํ ๊ฐ์ฒด ์์ ๋ฅผ ์คํํด๋ณด๋ผ.
์ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋๊ฐ?
๊ฐ์ฒด์ ๋ํด ์ฃผ์๋ฅผ ๊ธฐ์ธ์ด๋ ๋จ ํ๋ ๊ฐ์ฅ ์ค์ํ ์ด์ ๋, ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํจ์ผ๋ก์ ๋น์ ์ ์ฝ๋์์ ๋ฐ์ดํฐ์ ์ฒ๋ฆฌ๊ณผ์ ์ ๋ ์ ํํํ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค. ์ต์ํ ์์ ๋ก์, ๋น์ ์ด ์ผ๊ฐํ์ ๊ดํ ๋ญ๊ฐ๋ฅผ ์ฝ๋ฉํ๋ค๊ณ ๊ฐ์ ํ์. ์ผ๊ฐํ์ 3๊ฐ์ ๋ณ ์ ๊ฐ์ง๊ณ ์๋ค๋ ๊ฒ์ ์๊ณ ์์ผ๋ฏ๋ก, ํน์ ํ ์ผ๊ฐํ์ ๋ํด ๋ค๋ฃจ๋ ค๋ฉด ๋น์ฐํ 3๊ฐ์ ๋ณ์๋ฅผ ๋ง๋ค์ด์ผ ํ ๊ฒ์ด๋ค:
// This is a triangle.
var sideA = 3;
var sideB = 4;
var sideC = 5;
์, ์ผ๊ฐํ์ด ์ค๋น๋์๋ค. ํ์ง๋ง ์์ง ๋ช ํํ์ง๋ ์๋ค. ๋ฐ๋ก๋ฐ๋ก ์ถ์ ํด์ผ ํ ์ธ๊ฐ์ ๋ณ์๋ฅผ ์ด์ ๋ง๋ค์๊ณ , ๋น์ ์ด ์ด๊ฒ์ ๊ฐ์ง๊ณ ๋ฌด์์ ์ด๋ป๊ฒ ํ๋ ค๊ณ ํ์๋๋ฅผ ๋ ์ฌ๋ฆฌ๊ฒ ํด ์ค ์ฃผ์์ด ์๋ค. ์ด๊ฒ์ ๊ทธ๋ค์ง ๋ช ํํ๊ฑฐ๋, ์ฌ์ฉํ ๋งํ๋ค๊ณ ํ ์๋ ์๋ค. ์ด์จ๋ , ๊ณ์ํด๋ณด์. ์ด โ์ผ๊ฐํโ์ ๋ํด ์ด๋ค ๊ณ์ฐ์ ํ ์ ์์๊น? ๋ฉด์ ์ ์๊ธฐ ์ํด์, ๋ค์๊ณผ ๊ฐ์ ํจ์๋ฅผ ์ฌ์ฉํ ์ ์์ ๊ฒ์ด๋ค:
function getArea( a, b, c ) {
// Heron์ ๊ณต์์ ์ด์ฉํด์ ์ผ๊ฐํ์ ๋ฉด์ ์ ๊ณ์ฐํ๋ค.
var semiperimeter = (a + b + c) / 2;
var calculation = semiperimeter * (semiperimeter - a) * (semiperimeter - b) * (semiperimeter - c);
return Math.sqrt( calculation );
}
alert( getArea( sideA, sideB, sideC ) );
๋ญ๊ฐ ๊ณ์ฐ์ ํ๊ธฐ ์ํด์๋ ์ผ๊ฐํ์ ๋ํ ๋ชจ๋ ์ ๋ณด๋ฅผ ์ ๋ฌํด์ฃผ์ด์ผ ํ๋ค๋ ๊ฒ์ ๊นจ๋ฌ์์ ๊ฒ์ด๋ค. ์ผ๊ฐํ๊ณผ ๊ด๋ จ๋ ํ๋(๊ณ์ฐ)์ ์ผ๊ฐํ์ ๋ฐ์ดํฐ๋ก๋ถํฐ ์์ ํ ๋ถ๋ฆฌ๋์ด ์๋๋ฐ, ์ด๋ฌํ ๊ณ ๋ฆฝ์๋ ๊ทธ๋ค์ง ์๋ฏธ๊ฐ ์์ด ๋ณด์ด์ง ์๋๋ค.
์ข ๋ ๋ณด์. ๋๋ ์ด ํจ์์ ๋ณ์๋ค์ ๋ํด ์๋นํ ๋ฒ์ฉ์ ์ธ ์ด๋ฆ์ ์ ํด์ฃผ์๋ค: getArea, sideA ์ ๊ฐ์ด ๋ง์ด๋ค. ๋ค์์ฃผ์, ์ง์ฌ๊ฐํ์ ๋ํด์ ๋์ํ๋๋ก ํ๋ก๊ทธ๋จ์ ํ์ฅํด์ผ ํจ์ ๊นจ๋ฌ์๋ค๋ฉด ์ด๋ป๊ฒ ํ ๊ฒ์ธ๊ฐ? ์๋ง๋, ์ฌ๊ฐํ์ ์ฌ์ฉํ๊ธฐ ์ํด sideA ์ sideB ๊ฐ์ ๋ณ์๋ฅผ ์ฌ์ฉํ๋ ค๊ณ ํ๊ฒ ์ง๋ง, ์ด๋ฌํ ๋ณ์๋ ์ด๋ฏธ ์ฌ์ฉ๋์๋ค. side1 ๊ณผ side2 ๋ฅผ ์ฌ์ฉํ ์๋ ์๊ฒ ์ง๋ง, ์ด๋ฐ ๋ณ์๋ฅผ ์ฌ์ฉํจ์ผ๋ก์ ํผ๋๊ณผ ์ฌ๋์ ์ด๋ํ ๊ฒ์ด ๋ถ๋ช ํจ์, ๋น์ ๋ ์ด๋ฏธ ์ง์ํ ๊ฒ์ด๋ผ๊ณ ์๊ฐํ๋ค. ์๋ง๋, rectangleSideA, rectangleSideB ๊ฐ์ ๋ณ์๋ค์ ์๋ํด ๋ณด๋ค๊ฐ ๋ชจ์ ์์ ๋น ์ ธ๋ค๊ฒ ๋ ๊ฒ ๊ฐ๋ค. ์ผ๊ฐํ์ ๋ํด ์ด๋ฏธ ๋ง๋ค์ด ๋ ์ฝ๋๋ฅผ ๋ค์ ์์ ํด์ triangleSideA ๊ฐ์ ๊ฒ์ ์ฐ๊ฒ๋ ํ๋ค๊ฐ, ํ๊ฒฝ ์๋ฌ๋ฅผ ๋ง๋ค์ด ๋ผ ๊ฒ์ด๋ค. ์ด๋ฌํ ์์์ ๊ณผ ์ค๋ฅ๋ ๋ฌผ๋ก ํจ์์ ์ด๋ฆ์๋ ๊ณ์๋๋ค โ ๋๊ฐ์ ๋ค๊ฐํ์ ๋ํด์, ๊ทธ ๊ณ์ฐ์ด ๊ฐ๋ ์ ์ผ๋ก๋ ๊ฐ์ ๊ฒ์ด๊ธฐ ๋๋ฌธ์, ๋๊ฐ์ด getArea ๋ผ๋ ์ด๋ฆ์ ์ฌ์ฉํ๊ณ ์ถ์ง๋ง, ๊ทธ๋ ๊ฒ ํ ์ ์๋ค. ๋ฐ์ดํฐ๋ฅผ ํํํ ์ ์๋ ๋ ์ข์ ๋ฐฉ๋ฒ์ด ์์๊ฒ์ด๋ค.
๋ช๊ฐ์ ๋ช ๋ น์ด๋ค์ ๋ฌถ์ด์ ๋จ์ผํ, ์ ๋นํ ์ด๋ฆ์ ๊ฐ์ง ํ๋(ํจ์)์ ๋ง๋ค์ด ๋ด๋ ๊ฒ์ด ์ด์น์ ๋ง๋๋ค. ๋ง์ฐฌ๊ฐ์ง๋ก, ๋ช๊ฐ์ง โ๊ฒโ ๋ค์ ํ๋์ ๋จ์๋ก ๋ฌถ์ด์ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด๋ด๋ ๊ฒ์ด ์ด์น์ ๋ง๋๋ค. ์๋ฐ์คํฌ๋ฆฝํธ์์ ๋ฏธ๋ฆฌ ์ ์๋ ๊ธฐ์ด์ ๋ฐ์ดํฐ ํ์ (๋ฌธ์์ด, ์์, ๋ถ๋ฆฐ ๋ฑ)์ ์ ํ๋ฐ๋ ๋์ , ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด ์ฌ์ฉํจ์ผ๋ก์ ๋น์ ๋ง์ ๋ฐ์ดํฐ ํ์ ์ฐ๊ฒฐ์ ๋ง๋ค์ด ๋ผ ์ ์๋ค. ๊ฐ์ฒด์ ์ฌ์ฉ๋๋ ๋ณ์๋ค์ ์ซ์๋ ํ์ ์ ์ ํ์ ๋ฐ์ง ์๋๋ค. ์ด๋ ๊ฒ, ํ์์ ์ฝ๋งค์ด์ง ์๋ ์ ์ฐํจ์, ํ๋ก๊ทธ๋จ์ ์์ฑํ ๋ ๋น์ ์ด ๋ค๋ฃจ๋ ค๊ณ ํ๋ ๊ฒ์ ์ง์ ์ ์ผ๋ก ๋ค๋ฃจ๋ ๊ตฌ์กฐ๋ฅผ ๋ง๋ค์ด๋ผ ์ ์๊ฒ ํ๋ฉฐ, ๋ํ ๊ทธ๊ฒ๋ค์ ์๋ฐ์คํฌ๋ฆฝํธ์ ๋ด์ฅ๋ ๊ธฐ์ด์ ์ธ๊ฒ๋ค๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ์ฌ์ฉํ ์ ์๊ฒ ํด์ค๋ค. ์ด์ ์ผ๊ฐํ๊ณผ ์ง์ฌ๊ฐํ ๊ฐ์ฒด๋ฅผ ๋ง๋ค ๊ฒ์ธ๋ฐ, ๊ฐ๊ฐ์ ๊ฐ์ฒด๋ ๊ทธ ๋ชจ์์ ๋ค๋ฃจ๊ธฐ ์ํด ํ์ํ ๋ชจ๋ ๋ฐ์ดํฐ๋ค์ ํฌํจํ ๊ฒ์ด๋ฉฐ, ๋ํ ๊ทธ ๋ฐ์ดํฐ๋ค์ ๊ฐ์ง๊ณ ์ํํ๋ ค๊ณ ํ๋ ๋ชจ๋ ํ๋๋ค๋ ํจ๊ป ํฌํจํ ๊ฒ์ด๋ค. ์ด๋ฌํ ๋ชฉ์ ์ ์ผ๋์ ๋๊ณ , ๋ฌธ๋ฒ์ ๋ณด๋๋ก ํ์.
์ต์ํ ์์ญ
์ด์ ๊ธ์ ๋ง์ง๋ง ์ค์ต์ ๋ณด๋ฉด, ๋ค์๊ณผ ๊ฐ์ ์ฝ๋๊ฐ ํฌํจ๋์ด ์๋ค:
var obj = document.getElementById( elementID );
๊ทธ๋ฆฌ๊ณ :
obj.style.background = 'rgb('+red+','+green+','+blue')';
์, ๊ฐ์ฒด๊ฐ ๋ฌด์์ธ์ง ๋ชฐ๋์ง๋ง ์ด๋ฏธ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๊ณ ์์๋ค. ์ด ๋๊ฐ์ ์กฐ๊ฐ์ ์ข ๋ ์์ธํ ๋ถ์ํด์ ์๋ฐ์คํฌ๋ฆฝํธ์ ๊ฐ์ฒด ๋ฌธ๋ฒ์ ๋ํด ์ดํด๋ณด๋๋ก ํ์.
var obj = document.getElementById( elementID ) ์ด ์ฝ๋๋ ์ต์ํด ๋ณด์ธ๋ค. ๋ช ๋ น์ด ๋ง์ง๋ง์ ํฌํจ๋์ด ์๋ ๊ดํธ๋ ๋ญ๊ฐ๊ฐ ์คํ๋ ๊ฒ์ด๋ผ๋ ์๋ฏธ์ด๊ณ , ์ด ์คํ์ ๊ฒฐ๊ณผ๊ฐ obj ๋ผ๋ ๋ณ์์ ์ ์ฅ๋ ๊ฒ์ด๋ผ๊ณ ์ง์ํ ์ ์๋ค. ์ฌ๊ธฐ์์ย ์๋ก์ดย ๊ฒ์ ๋ฑ ํ๋, ์ค๊ฐ์ ์๋ ์ ์ด๋ค. ์ด๋ฌํย ์ ํ๊ธฐ๋ฒ์ ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ๊ฐ์ฒด ์์ ์ ์ฅ๋ ๋ฐ์ดํฐ์ ์ ๊ทผํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด๋ค. ์ ์ + ๋ โ ์ ๋ง์ฐฌ๊ฐ์ง๋ก, ํผ์ฐ์ฐ์ ์ฌ์ด์ ์๋ ์ฐ์ฐ์์ด๋ค.
๊ด์ต์ ์ผ๋ก, ๊ฐ์ฒด ์์ ์ ์ฅ๋๊ณ ์ ์ฐ์ฐ์๋ฅผ ํตํด ์ ๊ทผํ๋ ๋ฐ์ดํฐ๋ฅผ property ๋ผ๊ณ ๋ถ๋ฅธ๋ค. ํ๋กํผํฐ๋ ํจ์๊ฐ ๋ ์ ์๋๋ฐ, ์ด ๊ฒฝ์ฐ method ๋ผ ๋ถ๋ฅธ๋ค. ์ด๋ฌํ ๋จ์ด์ ํน๋ณํ ์๋ฏธ๋ ์๋ค โ ๋ฉ์๋๋ ํจ์์ผ ๋ฟ์ด๊ณ , ํ๋กํผํฐ๋ ๋ณ์์ผ ๋ฟ์ด๋ค.
์ ์ฐ์ฐ์์ ์ผ์ชฝ์๋ ๊ฐ์ฒด๊ฐ, ์ค๋ฅธ์ชฝ์๋ ํ๋กํผํฐ๊ฐ ๋์ธ๋ค. ์์ ์ฝ๋์ ์ด๋ฌํ ๊ท์น์ ์ ์ฉํด ๋ณธ๋ค๋ฉด, ์๋ฐ์คํฌ๋ฆฝํธ์ ๋ด์ฅ๋์ด ์๋ document ๊ฐ์ฒด์ getElementById ๋ฉ์๋์ ์ ๊ทผํ๋ค๊ณ ๋งํ ์ ์์ ๊ฒ์ด๋ค. ์ด๊ฒ์ ๊ดํด ๋ ์์ธํ ๋ด์ฉ์ ์ด์ด์งย DOM ์ฌํย ๊ธ์์ ํ์ธํ ์ ์๋ค.
๋ค์ ์ฝ๋๋ ์กฐ๊ธ ๋ ํฅ๋ฏธ๋กญ๋ค: ์ด๊ฒ์ ์ ์ ๋๊ฐ ๊ฐ์ง๊ณ ์๋ค. ์๋ฐ์คํฌ๋ฆฝํธ์ ๊ฐ์ฒด ์ง์ ์ค์์ ์ ๋ง๋ก ํฅ๋ฏธ๋ก์ด ๊ฒ ์ค ํ๋๋, ์ ์ฐ์ฐ์ย ์ฒด์ธ์ ํตํด ๋ณต์กํ ๊ตฌ์กฐ ์์ผ๋ก ๋ค์ด๊ฐ๋ค๋ ๊ฒ์ด๋ค. ์งง๊ฒ ๋งํด์, ์ด๋ฌํ ์ฒด์ธ์ ๋ง์น var x = 2 + 3 + 4 + 5; ๋ฅผ ๊ณ์ฐํ๊ณ ๊ฐ์ผ๋กย 14๋ฅผ ๊ธฐ๋ํ๋ ๊ฒ๊ณผ ๊ฐ๋ค. ๊ฐ์ฒด ์ฐธ์กฐ๋ ์์ ์ ๋ฐํํ๊ณ , ์ด๋ฌํ ์ฒด์ธ์ด ์ผ์ชฝ์์ ์ค๋ฅธ์ชฝ์ผ๋ก ๊ณ์ ์ด์ด์ง๋ค(์น๊ตฌ๋ค์๊ฒ ์ด๊ฒ์ ์ค๋ช ํ๋ฉด์ ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ์ ์ฐ์ฐ์๋ฅผ โ์ข์ธก ์ฐ๊ฒฐ ์ฝ์ ์ฐ์ฐ์โ ๋ก ์ฌ์ฉํ๋ค๊ณ ํํํด์ ๊น์ ์ธ์์ ์ค ์ ์์ ๊ฒ์ด๋ค) ์ด ๊ฒฝ์ฐ, obj.style ์ด ๋จผ์ ํ๊ฐ๋๊ณ , ์ด๊ฒ์ ํด์ํ๋ฉด ๊ทธ๊ฒ์ background ํ๋กํผํฐ์ ์ ๊ทผํ๋ ๊ฐ์ฒด๊ฐ ๋๋ค. ๋น์ ์ด ์ํ๋ค๋ฉด, ๊ดํธ๋ฅผ ์ฌ์ฉํด์ ์ด๊ฒ์ ์ข ๋ ๋ช ์์ ์ผ๋ก ํํํ ์ ์๋ค: (obj.style).background.
๊ฐ์ฒด ์์ฑ
์ผ๊ฐํ ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ธฐ ์ํด, ๋ค์์ ๋ฌธ๋ฒ์ ์ฌ์ฉํด์ ๋ช ์์ ์ผ๋ก ์์ฑํ ๊ฒ์ด๋ค:
var triangle = new Object();
triangle ๋ ์ด์ ๋น์ด์๋ ๊ธฐ์ด์ด๋ฉฐ ๋ฌด์ธ๊ฐ๋ฅผ ๊ทธ ์์ ์์์ฌ๋ ค์ผ ํ๋ค. ์ ์ฐ์ฐ์๋ฅผ ํตํด ํ๋กํผํฐ๋ค์ ์ถ๊ฐํจ์ผ๋ก์ ๊ทธ๋ ๊ฒ ํ ์ ์๋ค:
triangle.sideA = 3;
triangle.sideB = 4;
triangle.sideC = 5;
๊ฐ์ฒด์ ์๋ก์ด ํ๋กํผํฐ๋ฅผ ์์ฑํ๊ธฐ ์ํด์ ๋ค๋ฅธ ํน๋ณํ ์ผ์ ํ ํ์๋ ์๋ค. ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ์ ์ฐ์ฐ์๋ฅผ ํ๊ฐํ ๋, ์ด๊ฒ์ ๋๋จํ ๊ด๋ํ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ค. ์กด์ฌํ์ง ์๋ ํ๋กํผํฐ์ ์ ๊ทผํ๋ ค๊ณ ์๋ํ๋ค๋ฉด, ์๋ฐ์คํฌ๋ฆฝํธ๋ ๊ทธ๊ฒ์ ์๋ก ๋ง๋ค์ด๋ธ๋ค. ์กด์ฌํ์ง ์๋ ํ๋กํผํฐ๋ฅผ ์ฝ์ผ๋ ค ํ๋ค๋ฉด ์๋ฐ์คํฌ๋ฆฝํธ๋ โundefinedโ ๊ฐ์ ๋ฐํํ๋ค. ์ด๊ฒ์ ํธ๋ฆฌํ์ง๋ง, ์ฃผ์๊น๊ฒ ์ฌ์ฉํ์ง ์๋๋ค๋ฉด ์๋ฌ๋ฅผ ๋ง๋ค์ด ๋ธ๋ค. ๋ฐ๋ผ์ ์คํ์ ์ฃผ์ํ๋๋ก ํ์.
๋ฉ์๋๋ฅผ ์ถ๊ฐํ๋ ๊ฒ ์ญ์ ๋น์ทํ๋ค โ ์์ ๋ฅผ ๋ณด์:
triangle.getArea = function ( a, b, c ) {
// Return the area of a triangle using Heron's formula
var semiperimeter = (a + b + c) / 2;
var calculation = semiperimeter * (semiperimeter - a) *
(semiperimeter - b) * (semiperimeter - c);
return Math.sqrt( calculation );
}; // ์ด ์ค์ ์ฌ์ฉ๋ ์ธ๋ฏธ์ฝ๋ก ์ ๋์ฌ๊ฒจ ๋ณด๊ธฐ ๋ฐ๋๋ค. ์ด๊ฒ์ ๊ท์น์ ์ผ๋ถ์ด๋ค.
์ด๊ฒ์ด ํจ์ ์ ์ธ๊ณผ ๋ฌด์ฒ ๋น์ทํ๋ค๊ณ ์๊ฐํ๋ค๋ฉด, ์ ๋๋ก ๋ณด๊ณ ์๋ ๊ฒ์ด๋ค. ๊ทธ์ ํจ์ ์ด๋ฆ์ด ๋น ์ก์ ๋ฟ์ด๋ค. ์๋ฐ์คํฌ๋ฆฝํธ๋ ์ต๋ช ํจ์๋ผ๋ ๊ฐ๋ ์ ๊ฐ๊ณ ์๋๋ฐ, ํจ์๋ ์ด๋ฆ์ ๊ฐ์ง๋ ๋์ ๋ค๋ฅธ ๊ฐ๋ค๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ณ์์ ์ ์ฅ๋๋ค. ์ด ์ฝ๋์์, ๋๋ ์ต๋ช ํจ์๋ฅผ ๋ง๋ค์ด๋ด๊ณ ๊ทธ๊ฒ์ triangle ๊ฐ์ฒด์ getArea ํ๋กํผํฐ์ ์ ์ฅํ๋ค. ์ด์ ๊ฐ์ฒด๋ ๋ฐ์ดํฐ์ ํจ๊ป ํจ์๋ฅผ ๋ฐ๋ฆฌ๊ณ ๋ค๋๋ค.
์๊ฐ ์ฐธ์กฐ
์ผ๊ฐํ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด ๋ธ ๋ชฉ์ ์ค ํ๋๋, ์ผ๊ฐํ์ ๋ฐ์ดํฐ์, ๊ทธ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง๊ณ ํ ๊ณ์ฐ ์ฌ์ด์ ๊ด๊ณ๋ฅผ ๋ง๋ค์ด ๋ด๋ ์ผ์ด์๋๋ฐ, ์์ง ๊ทธ๊ฒ์ ์์ฑํ์ง ๋ชปํ๋ค. triangle.getArea ๋ฉ์๋๊ฐ ์ฌ์ ํ ๊ฐ ๋ณ์ ๊ธธ์ด๋ฅผ ํ์๋ก ํ๊ณ ์์์ ์ ์ ์์ ๊ฒ์ด๋ค. ๋ฐ๋ผ์ ์ด๋ฐ ์์ผ๋ก ๋ช ๋ นํด์ผ ํ๋๋ฐ:
triangle.getArea( triangle.sideA, triangle.sideB, triangle.sideC );
์ด๊ฒ์ด ์ฒ์์ ํ๋ ์ฝ๋๋ณด๋ค๋ ๋์๋ฐ, ์๋ํ๋ฉด ์ด๊ฒ์ ๋ฐ์ดํฐ์ ๊ณ์ฐ ์ฌ์ด์ ๊ด๊ณ๋ฅผ ๋ช ํํ๊ฒ ํํํ๊ณ ์๊ธฐ ๋๋ฌธ์ด๋ค. ํ์ง๋ง, ๊ทธ ๊ด๊ณ๋, ๋ฉ์๋์๊ฒ ์ด๋ค ๊ฐ์ ๊ฐ์ง๊ณ ๊ณ์ฐํ ์ง๋ฅผ ๋งค๋ฒ ์๋ ค์ค ํ์๊ฐ ์๋ ๊ฒ์ด์ด์ผ ํ๋ค. ๋ฉ์๋๋ ์์ ์ ํฌํจํ๊ณ ์๋ ๊ฐ์ฒด ์์์ ๋ฐ์ดํฐ๋ฅผ ์์งํ ์ ์์ด์ผ ํ๊ณ , ์ฌ์ฉ์์๊ฒ ๊ฐ์ ์ ๋ ฅํ ๊ฒ์ ์๊ตฌํ์ง ์๊ณ ๊ทธ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
๋ฉ์๋๊ฐ ์์ ์ ํฌํจํ ๊ฐ์ฒด ์์์ ๋ฐ์ดํฐ๋ฅผ ์์งํ ์ ์๊ฒ ํ๋ ๋น๊ฒฐ์ this ํค์๋ ์์ ์๋ค. ๋ฉ์๋๋ฅผ ์ ์ํ ๋ ์ด ํค์๋๋ฅผ ์ฌ์ฉํจ์ผ๋ก์, ๋ฉ์๋๊ฐ ์คํ๋ ๋ ๊ฐ์ ๊ฐ์ฒด ๋ด์ ๋ค๋ฅธ ํ๋กํผํฐ๋ ๋ฉ์๋๋ฅผ ์ฐธ์กฐํ๋๋ก ์ง์ ํ ์ ์๋ค. this ๋ฅผ ์ฌ์ฉํ๋๋ก getArea ๋ฅผ ๋ค์ ์์ ํ๋ฉด ๋ค์๊ณผ ๊ฐ๋ค:
triangle.getArea = function () {
// Return the area of a triangle using Heron's formula
var semiperimeter = (this.sideA + this.sideB + this.sideC) / 2;
var calculation = semiperimeter * (semiperimeter - this.sideA) * (semiperimeter - this.sideB) * (semiperimeter - this.sideC);
return Math.sqrt( calculation );
}; // Note the semi-colon here, it's mandatory.
์ฌ๊ธฐ์ ๋ณด๋ ๋ฐ์ ๊ฐ์ด, this ํค์๋๋ ๊ฑฐ์ธ๊ณผ ๋น์ทํ๊ฒ ๋์ํ๋ค. getArea ๋ฉ์๋๊ฐ ์คํ๋ ๋, ๋ฉ์๋๋ ๊ฐ์ฒด ๋ด๋ถ์์ sideA, sideB, sideC ํ๋กํผํฐ๋ฅผ ๊ฒ์ํ๊ฒ ๋๋ค. ๊ฐ์ฒด ๋ด๋ถ์ ํ๋กํผํฐ๊ฐ ์ด๋ฏธ ์ ์๋์ด ์์ผ๋ฏ๋ก, ์ธ๋ถ์์ ์ ๋ ฅํ๋ ๊ฐ์ ์์กดํ์ง ์๊ณ ๊ณ์ฐ์ ํ ์ ์๋ค.
์ด๊ฒ์ ์กฐ๊ธ ๊ณผํ๊ฒ ๋จ์ํ์ํจ ํํ์ด๋ค. this ํค์๋๊ฐย ์ธ์ ๋ย ๋ฉ์๋๋ฅผ ํฌํจํ๋ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๋ ๊ฒ์ ์๋๋ค โ ์ฌ์ค ์ด ํค์๋๋ ์์ ์ ํธ์ถํ๋ ๋ฌธ๋งฅ์ ๊ธฐ๋ฐํด์ ๋์ํ๋ค. ๋ชจํธํจ์ ๋ํด ์ฌ๊ณผ๋๋ฆฐ๋ค. ํ์ง๋ง, ์ด๊ฒ์ ๋ค๋ฃจ๋ ๊ฒ์ ์ด ๊ธ์ ์ด์ ์ ๋ฒ์ด๋๋ ์ผ์ด ๋ ๊ฒ์ด๋ค. ์๋ฌดํผ, ์ด ๊ธ์์๋, this ํค์๋๋ ํญ์ triangle ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ ๊ฒ์ด๋ค.
์ฐ๊ด๋ ๋ฐฐ์ด๋ก์์ ๊ฐ์ฒด
๊ฐ์ฒด์ ํ๋กํผํฐ์ ๋ฉ์๋์ ์ ๊ทผํ๋ ๋ฐฉ๋ฒ์๋ ์ ์ฐ์ฐ์๋ง์ด ์๋๊ฒ์ ์๋๋ค. ์ด๋ฏธ ๋ค๋ฃจ์๋ ๊ธ,ย ๋ฐฐ์ด์ ๋ํ์ฌ๋ฅผ ํตํด์ ์ถฉ๋ถํ ์ต์ํด์ ธ ์์,ย ๋๊ดํธ ํ๊ธฐ๋ฒscript notation์ ์ฌ์ฉํด์ ๋ ํจ์จ์ ์ผ๋ก ์ ๊ทผํ ์ ์๋ค. ์งง๊ฒ ๋งํด, ๋น์ ์ ๊ฐ์ฒด๋ฅผย ์ฐ๊ด๋ย โ ์ผ๋ฐ์ ์ธ ๋ฐฐ์ด์ด, ๊ฐ์ ์ซ์๋ฅผ ํ ๋นํ๋ ๊ฒ๊ณผ๋ ๋์กฐ์ ์ผ๋ก ๊ฐ์ ๋ฌธ์์ด์ ํ ๋นํ๋ โย ๋ฐฐ์ด์ด๋ผ๊ณ ์๊ฐํด๋ ๋ฌด๋ฐฉํ๋ค๋ ๊ฒ์ด๋ค. ์ด๋ฌํ ํ๊ธฐ๋ฒ์ ์ฌ์ฉํด์, triangle ๊ฐ์ฒด๋ฅผ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก ์์ฑํ ์ ์๋ค:
var triangle = new Object();
triangle['sideA'] = 3;
triangle['sideB'] = 4;
triangle['sideC'] = 5;
triangle['getArea'] = function ( a, b, c ) {
// Return the area of a triangle using Heron's formula
var semiperimeter = (a + b + c) / 2;
var calculation = semiperimeter * (semiperimeter - a) * (semiperimeter - b) * (semiperimeter - c);
return Math.sqrt( calculation );
}; // Note the semi-colon here, it's mandatory.
์ธ๋ป ๋ณด๊ธฐ์๋, ์ด๊ฒ์ ๋ถํ์ํ๊ฒ ๊ธธ๊ฒ ์ด ๊ฒ์ผ๋ก ๋ณด์ผ ์ ์๋ค. ์ ๊ฐ๋จํ ์ ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ง ์๋๊ฐ? ์ด๋ฌํ ์๋ก์ด ๋ฌธ๋ฒ์ ์ฅ์ ์, ํ๋กํผํฐ์ ์ด๋ฆ์ด ํ๋ก๊ทธ๋จ ์์์ ์์ ํ๊ฒ ์ ์๋ ํ์๊ฐ ์๋ค๋ ๊ฒ์ด๋ค. ํ๋กํผํฐ์ ์ด๋ฆ์ ๋ช ์ํ๊ธฐ ์ํด ๋ณ์๋ฅผ ์ฌ์ฉํ ์ ์๊ณ , ์ด๋ฌํ ๊ฒ์ ํ์ฉํจ์ผ๋ก์ ํ๋ก๊ทธ๋จ์ ์ฐธ์ผ๋ก ์ ์ฐํด์ง ์ ์๋ค โ ๋ฌธ๋งฅ์ ๊ธฐ์ดํด์ ๋ค์ํ ๋์์ ์ํํ ์ ์๋ค๋ ๋ป์ด๋ค. ์๋ฅผ ๋ค์ด, ๋๊ฐ์ ๊ฐ์ฒด๊ฐ ํ๋์ ํ๋กํผํฐ๋ฅผ ๊ณต์ ํ๋์ง ๋น๊ตํด ๋ณผ ์ ์๋ ํจ์๋ฅผ ์์ฑํ ์ ์๋ค:
function isPropertyShared( objectA, objectB, propertyName ) {
if (
typeof objectA[ propertyName ] !== undefined
&&
typeof objectB[ propertyName ] !== undefined
) {
alert("Both objects have a property named " + propertyName + "!");
}
}
์ด๋ฌํ ํจ์๋, ์ ํ๊ธฐ๋ฒ์ ์ฌ์ฉํด์๋ ์ ๋ ๋ง๋ค์ด ๋ผ ์ ์๋๋ฐ, ์ ์ฐ์ฐ์ ๋ค์๋ ํ๋กํผํฐ์ ์ด๋ฆ์ ๋ช ์์ ์ผ๋ก ์ ์ด์ฃผ์ด์ผ๋ง ํ๊ธฐ ๋๋ฌธ์ด๋ค. ๋น์ ์ ์ด ๋ฌธ๋ฒ์ย ์์ฃผ ๋ง์ดย ์ฌ์ฉํ๊ฒ ๋ ๊ฒ์ด๋ค.
์ฐธ๊ณ : ์ฐ๊ด๋ ๋ฐฐ์ด์ย Perlย ์ธ์ด์์๋ ํด์ฌ ๋ผ๊ณ ๋ถ๋ฅด๋ฉฐ, C# ์์๋ ํด์ฌํ ์ด๋ธ, C++ ์์๋ ๋งต, ์๋ฐ ์์๋ ํด์ฌ๋งต, Python ์ธ์ด์์๋ ๋์ ๋๋ฆฌ, ์ด๋ ๊ฒ ๋ค์ํ๊ฒ ํํ๋๋ค. ์ด๊ฒ์ ์์ฃผ ๊ฐ๋ ฅํ๋ฉด์๋ ๊ธฐ์ด์ ์ธ ํ๋ก๊ทธ๋๋ฐ ๊ธฐ๋ฒ์ด๊ณ , ์ด๊ฒ์ ๋ค๋ฅธ ์ด๋ฆ์ ์ด๋ฏธ ์๊ณ ์์์ ์๋ ์๋ค.
๊ฐ์ฒด ๋ฆฌํฐ๋ด
์์ฃผ ์น์ํ ์ฝ๋๋ฅผ ์ข ๋ ์์ธํ ๋ค์ฌ๋ค๋ณด๋๋ก ํ์:
alert(โHello worldโ);
alert๋ ํจ์์ด๊ณ , โHello worldโ ๋ผ๋ ๋ฌธ์์ด์ ์ธ์๋ก ๊ฐ์ง๊ณ ํธ์ถ๋๊ณ ์๋ค๋ ๊ฒ์ ์ ์ ์๋ค. ์ฌ๊ธฐ์์ ๊ฐ์กฐํ๊ณ ์ ํ๋ ๊ฒ์, ์ด๋ ๊ฒ ์ธย ํ์๊ฐ ์๋ค๋ ๊ฒ์ธ๋ฐ:
var temporaryString = "Hello world";
alert(temporaryString);
์๋ฐ์คํฌ๋ฆฝํธ๋, ๋ฐ์ดํ(โ โ)๋ก ๋๋ฌ์ธ์ธ ๋ชจ๋ ๊ฒ์ ๋ฌธ์์ด๋ก ์ทจ๊ธ๋์ด์ผ ํ๋ค๊ณ ์ดํดํ๋ฉฐ, ๊ทธ๊ฒ์ด ์ด๋์ ์ฐ์๋ ๊ฐ์ ์ฌ๋ฐ๋ฅด๊ฒ ์ฒ๋ฆฌํ ์ ์๋ ํ์ํ ๋ชจ๋ ์ผ์ ๋ฐฑ๊ทธ๋ผ์ด๋๋ก ์ฒ๋ฆฌํ๋ค. ๋ฌธ์์ด์ด ๋ง๋ค์ด์ง๊ณ , ๊ทธ๊ฒ์ด ํจ์์ ์ธ์๋ก ์ ๋ฌ๋๋ ๊ณผ์ ์ด ํ๋ฒ์ ์ด๋ฃจ์ด์ก๋ค. ์ ํ์ ์ผ๋ก๋, โHello worldโ ์ด๊ฒ์ ๋ฌธ์์ด ๋ฆฌํฐ๋ด ์ ์ฐธ์กฐํ๋ค: ๋ฌธ์์ด์ ์์ฑํ๊ธฐ ์ํด ํ์ํ ๋ชจ๋ ๊ฒ์ ์์ ํ๊ฒ ์ ๋ ฅํ ๊ฒ์ด๋ค.
์๋ฐ์คํฌ๋ฆฝํธ๋, โ๊ฐ์ฒด ๋ฆฌํฐ๋ดโ ์ ๋ํด์๋ ๋น์ทํ ๋ฌธ๋ฒ์ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ ์ด๊ฒ์ ํตํด ๋ฌธ๋ฒ์ ์ธ ์ ์ฝ์ ์ง๋์น๊ฒ ๊ตฌ์ ๋ฐ์ง ์๊ณ ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์๋ค. ๊ฐ์ฒด ๋ฆฌํฐ๋ด์ ๊ฐ์ง๊ณ , ์์์ ๋ง๋ค์ด ๋ธ ๊ฐ์ฒด๋ฅผ ๋ค์ ์์ฑํด ๋ณด์:
var triangle = {
sideA: 3,
sideB: 4,
sideC: 5,
getArea: function ( a, b, c ) {
// Return the area of a triangle using Heron's formula
var semiperimeter = (a + b + c) / 2;
var calculation = semiperimeter * (semiperimeter - a) * (semiperimeter - b) * (semiperimeter - c);
return Math.sqrt( calculation );
}
};
์ญ์ฃผ : ์ด๋ฌํ ํ๊ธฐ๋ฒ์ JavaScript Object Notation โ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด ํ๊ธฐ๋ฒ โ JSON ์ด๋ผ๊ณ ๋ถ๋ฅธ๋ค.
์ด๋ฌํ ๋ฌธ๋ฒ์ ๋ช ํํ๋ค: ๊ฐ์ฒด ๋ฆฌํฐ๋ด์ ๊ฐ์ฒด์ ์์๊ณผ ๋์ ์ค๊ดํธ๋ก ๋๋ฌ์ธ๋ฉฐ, ์ค๊ดํธ ์์๋ ํ๋กํผํฐ ์ด๋ฆ : ํ๋กํผํฐ ๊ฐ ์์ด ์ฝค๋ง๋ก ๊ตฌ๋ถ๋์ด ๋์ด๋๋ค. ์ด๋ฌํ ๋ฌธ๋ฒ์ ์ฌ์ฉํจ์ผ๋ก์, ํ๋ก๊ทธ๋จ์ ๋งค ํ๋ง๋ค ๊ฐ์ฒด ์ด๋ฆ์ ๋ฐ๋ณตํ๋ ์ง๋ฃจํ ์์ ์์ ํด๋ฐฉ๋์ด ์ฝ๊ฒ ํ๋ก๊ทธ๋จ์ ๊ตฌ์กฐํํ ์ ์๋ค.
ํ๊ฐ์ง ์กฐ์ฌํ ๊ฒ์ด ์๋ค : ๊ฐ์ฒด ๋ฆฌํฐ๋ด์ ํ๋กํผํฐ ๋ฆฌ์คํธ ์ค ๋ง์ง๋ง ๊ฒ(์ฌ๊ธฐ์์๋, getArea) ๋ค์ ์ผํ๋ฅผ ๋ฃ๋ ์ค์๋ฅผ ์์ฃผ ํํ๊ฒ ๋ฒํ๋ค. ์ผํ๋ ํ๋กํผํฐ ์ฌ์ด ์๋ง ๋ฃ์ด์ผ ํ๋ค โ ๋ง์ง๋ง์ ๋ฃ์ด์ง ํ์์๋ ์ผํ๋ ์๋ฌ๋ฅผ ๋ง๋ ๋ค. ํนํ, ์ฝ๋๋ฅผ ์ดํ์ ์์ ํ๋ฉด์ ํ๋กํผํฐ๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์ง์ธ ๋ ์ด๋ฐ ์ค์๋ฅผ ๋ฒํ๊ธฐ ์ฌ์ฐ๋ฏ๋ก, ์ผํ๊ฐ ์ ํํ ์์น์ ์๋๋ก ์ฃผ์๋ฅผ ๊ธฐ์ธ์ผ ํ์๊ฐ ์๋ค.
์์ฝ โ ๋ฐฐ์ธ ๊ฒ์ด ๋ ๋ง์ด ์๋ค
์ด ๊ธ์, ์๋ฐ์คํฌ๋ฆฝํธ์์ ๊ฐ์ฒด๊ฐ ๊ฐ๋ ๊ฐ๋ฅ์ฑ๊ณผ ํ๊ณ์ ๋ํด์ ์ ๋ง๋ก ์๋ฐ ๊ฒํง๊ธฐ ์ ๋๋ฐ์๋ ๋์ง ์๋๋ค. ์ด ๊ธ์ ์์ ํ ์ฝ์๋ค๋ฉด, ์ค์ค๋ก ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ ํ๋กํผํฐ์ ๋ฉ์๋๋ฅผ ์ถ๊ฐํ๊ณ ๊ทธ๊ฒ์ ์๊ฐ์ฐธ์กฐ์ ํํ๋ก ์ฌ์ฉํ๋๋ฐ์๋ ์ต์ํด ์ง ๊ฒ์ด๋ค. ์ด๊ฒ ์ธ์๋ ์ ๋ง๋ก ๋ง์ ๊ฒ๋ค์ด ์์ง๋ง, ๊ทธ์ค ์ด๋ค๊ฒ๋ ํ์์ ์ด์ง๋ ์๋ค. ์ด ๊ธ์ ๊ธด ์ฌํ์ ์ถ๋ฐ์ ์ผ๋ก์ ๊ธฐํ๋์์ผ๋ฉฐ, ์ดํ ๋น์ ์ด ๊ธธ์ ํค์ณ๋๊ฐ๋๋ฐ ํ์๋ก ํ ๋๊ตฌ๋ฅผ ์ ๊ณตํ๋ ๋ชฉ์ ์ผ๋ก ์์ฑ๋์๋ค.
์ฝ์ด๋ณผ ๊ฒ๋ค
Object Oriented JavaScript: ์๋ฐ์คํฌ๋ฆฝํธ์ ๊ฐ์ฒด์งํฅ ์ปจ์ ์ ๋ํ ๋์ฑ ์์ธํ ์๊ฐ.
Private Members in JavaScript: ๋๊ธ๋ฌ์ค ํฌ๋กํฌ๋์, ์๋ฐ์คํฌ๋ฆฝํธ์์์ ์บก์ํ ๊ธฐ๋ฒ์ ๋ค๋ฃจ๋ ์ธ๋ฏธ๋ ํ์ ํ ์.ย 1
Scope in JavaScript: ๋ค์ํ ๋ฌธ๋งฅ์์ ์ฌ์ฉ๋๋ this ํค์๋์ ๋ณต์กํจ์ ๋ํ ์ข ๋ ์์ธํ ํ ์.
์ฐ์ต๋ฌธ์
๊ฐ์ฒด์ ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ๊ธฐ ์ํด ์ ํ๊ธฐ๋ฒ ๋์ ๋๊ดํธ ํ๊ธฐ๋ฒ์ ์ฌ์ฉํด์ผ ํ ๋๋ ์ด๋ค ๊ฒฝ์ฐ์ธ๊ฐ?
๊ฐ์ฒด๋ ์ด๋ป๊ฒ ์์ ์ ์ฐธ์กฐํ๋๊ฐ? ์ ์ด๊ฒ์ด ์ค์ํ๊ฐ?
๊ฐ์ฒด ๋ฆฌํฐ๋ด์ด๋ ๋ฌด์์ธ๊ฐ? ๊ฐ์ฒด ๋ฆฌํฐ๋ด์ ์์ฑํ ๋, ์ผํ๋ฅผ ์ด๋์ ์ฌ์ฉํด์ผ ํ๋๊ฐ?
์ผ๊ฐํ์ ํํํ๊ณ , ๋์ด๋ฅผ ๊ณ์ฐํ๋ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์๋ค. ์ง์ฌ๊ฐํ์ ๋ํด์๋ ๊ทธ๋ ๊ฒ ํด ๋ณด๊ธฐ๋ฅผ ๋ฐ๋๋ค. this ํค์๋๋ฅผ ์ฌ์ฉํด์, ์ง์ฌ๊ฐํ์ getArea ๋ฉ์๋๊ฐ ๊ทธ ๋ฐ์ดํฐ๋ฅผ ํ์ํ์ง ์์ ๊ณณ์ ์ ๋ฌํ๋ ๊ฒ์ ๋ง๋๋ก ํด ๋ณด๋ผ.

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch โข No registration required โข HD streaming
3 ways to define a JavaScript class
http://www.phpied.com/3-ways-to-define-a-javascript-class/ย ์์ ์ฐธ์กฐ
Introduction
JavaScript is a very flexible object-oriented language when it comes to syntax. In this article you can find three ways of defining and instantiating an object. Even if you have already picked your favorite way of doing it, it helps to know some alternatives in order to read other people's code.
It's important to note that there are no classes in JavaScript. Functions can be used to somewhat simulate classes, but in general JavaScript is a class-less language. Everything is an object. And when it comes to inheritance, objects inherit from objects, not classes from classes as in the "class"-ical languages.
1. Using a function
This is probably one of the most common ways. You define a normal JavaScript function and then create an object by using theย newย keyword. To define properties and methods for an object created usingfunction(), you use theย thisย keyword, as seen in the following example.
function Apple (type) { this.type = type; this.color = "red"; this.getInfo = getAppleInfo; } // anti-pattern! keep reading... function getAppleInfo() { return this.color + ' ' + this.type + ' apple'; }
To instantiate an object using the Appleย constructor function, set some properties and call methods you can do the following:
var apple = new Apple('macintosh'); apple.color = "reddish"; alert(apple.getInfo());
1.1. Methods defined internally
In the example above you see that the method getInfo() of the Apple "class" was defined in a separate function getAppleInfo(). While this works fine, it has one drawback โ you may end up defining a lot of these functions and they are all in the "global namespece". This means you may have naming conflicts if you (or another library you are using) decide to create another function with the same name. The way to prevent pollution of the global namespace, you can define your methods within the constructor function, like this:
function Apple (type) { this.type = type; this.color = "red"; this.getInfo = function() { return this.color + ' ' + this.type + ' apple'; }; }
Using this syntax changes nothing in the way you instantiate the object and use its properties and methods.
1.2. Methods added to the prototype
A drawback of 1.1. is that the method getInfo() is recreated every time you create a new object. Sometimes that may be what you want, but it's rare. A more inexpensive way is to add getInfo() to theย prototypeย of the constructor function.
function Apple (type) { this.type = type; this.color = "red"; } Apple.prototype.getInfo = function() { return this.color + ' ' + this.type + ' apple'; };
Again, you can use the new objects exactly the same way as in 1. and 1.1.
2. Using object literals
Literals are shorter way to define objects and arrays in JavaScript. To create an empty object using you can do: var o = {}; instead of the "normal" way: var o = new Object(); For arrays you can do: var a = []; instead of: var a = new Array(); So you can skip the class-like stuff and create an instance (object) immediately. Here's the same functionality as described in the previous examples, but using object literal syntax this time:
var apple = { type: "macintosh", color: "red", getInfo: function () { return this.color + ' ' + this.type + ' apple'; } }
In this case you don't need to (and cannot) create an instance of the class, it already exists. So you simply start using this instance.
apple.color = "reddish"; alert(apple.getInfo());
Such an object is also sometimes calledย singleton. It "classical" languages such as Java,ย singletonย means that you can have only one single instance of this class at any time, you cannot create more objects of the same class. In JavaScript (no classes, remember?) this concept makes no sense anymore since all objects are singletons to begin with.
3. Singleton using a function
Again with the singleton, eh?ย
The third way presented in this article is a combination of the other two you already saw. You can use a function to define a singleton object. Here's the syntax:
var apple = new function() { this.type = "macintosh"; this.color = "red"; this.getInfo = function () { return this.color + ' ' + this.type + ' apple'; }; }
So you see that this is very similar to 1.1. discussed above, but the way to use the object is exactly like in 2.
apple.color = "reddish"; alert(apple.getInfo());
new function(){...}ย does two things at the same time: define a function (an anonymous constructor function) and invoke it withย new. It might look a bit confusing if you're not used to it and it's not too common, but hey, it's an option, when you really want a constructor function that you'll use only once and there's no sense of giving it a name.
Summary
You saw three (plus one) ways of creating objects in JavaScript. Remember that (despite the article's title) there's no such thing as a class in JavaScript. Looking forward to start coding using the new knowledge? Happy JavaScript-ing!
"Counting stars" onerepublic
Advanced text searching using full-text indexes
http://www.tuxradar.com/practicalphp/9/3/18 ์์ ์ฐธ์กฐ
Advanced text searching using full-text indexes
Creating an index of large text fields is not practical in its default state - text fields have certain characteristics that warrant their own form of indexing. For example, many simple words ("at", "the", etc) are unimportant, and therefore aren't stored in FULLTEXT indexes. Furthermore, MySQL has an infamous "50% rule", whereby it will not bother indexing words that appear in the more than 50% of records simply because it will not be of use.
Creating a full text index is done after creating your table, using the following command: ALTER TABLE some_table ADD FULLTEXT (some_field). Here is an example:
ALTER TABLE usertable ADD FULLTEXT(FirstName);
Once we have a FULLTEXT index on our table, it opens up a whole new world of pattern matching, which, as of MySQL 4, allows us to do boolean matching with little fuss. FULLTEXT queries look a little more complicated that normal SELECT queries, but are much more powerful - they can even be used to return "match quality", as you will soon see. To properly demonstrate FULLTEXT indexes, we need a table with lots of text in:
CREATE TABLE opinions (Opinion CHAR(100)); INSERT INTO opinions VALUES ('MySQL is a very fast database'); INSERT INTO opinions VALUES ('Green is everyone\'s favourite colour'); INSERT INTO opinions VALUES ('Databases are helpful for storing data'); INSERT INTO opinions VALUES ('PHP is a very nice language'); INSERT INTO opinions VALUES ('Spain is a nice country to visit'); INSERT INTO opinions VALUES ('Perl isn\'t as nice a language as PHP'); INSERT INTO opinions VALUES ('This is a blank row to avoid the 50% rule'); ALTER TABLE opinions ADD FULLTEXT (Opinion);
That gives us a very basic table with a FULLTEXT index on the opinion text. From there, we can jump in with a very basic SELECT query to match all rows with "nice" in:
SELECT * FROM opinions WHERE MATCH(Opinion) AGAINST ('nice');
That query generates the following output:
+---------------------------------------+ | Opinionย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย | +---------------------------------------+ | PHP is a very nice languageย ย ย ย ย ย ย ย ย ย ย | | Spain is a nice country to visitย ย ย ย ย ย | | Perl is not as nice a language as PHP | +---------------------------------------+
As you can see, the query has indeed matched the three rows that contain the string "nice". One important thing to note is that the minimum size of word that MySQL will index is, by default, four characters - anything smaller than that is rarely worthwhile.
Now consider this second query:
SELECT * FROM opinions WHERE MATCH(Opinion) AGAINST ('nice language');
What do you think it will output? If you thought it would output just one row, you would be wrong - MySQL uses OR by default when matching words, meaning it will return any row that matches "nice" or matches "language" - this is where boolean mode searches come in.
Boolean mode searching has long been popular with internet search engines - they allow you to proceed words with a + or a - to force it to either be present (+) or not present (-). You can switch to boolean mode searching using FULLTEXT indexes by adding "IN BOOLEAN MODE" to the query, like this:
SELECT * FROM opinions WHERE MATCH(Opinion) AGAINST ('nice -language' IN BOOLEAN MODE);
Note that the "IN BOOLEAN MODE" modifier comes inside the AGAINST brackets. This time the query returns just one result - "Spain is a nice country to visit". This is because "-language" means that MySQL will not return any rows that match "language", even if they match "nice". Searching in MySQL has a number of other ways it can be used - for example, putting double quotes around groups of words allow phrase searching. That is, to match precisely "nice language", we would use the following query:
SELECT * FROM opinions WHERE Match(Opinion) AGAINST ('"nice language"' IN BOOLEAN MODE);
That matches rows that have "nice language" just like that - no words in between, not one or the other. As such, only "PHP is a very nice language" is matched.
There are other boolean operators, although they get more complicated. For example, > and < mark that the following word has a higher or lower relevance respectively than other words. The tilde symbol, ~, means that the following word should contribute negatively to the relevance of the row - this is not the same as <, which marks a word as being less relevant but still relevant nonetheless, and -, which excludes a word altogether. The asterisk symbol, *, allows wildcard matching. Finally, you can use parentheses, ( and ), to group words into subexpressions.
So, all of these examples are possible boolean queries:ย
+nice +language
Match both nice and language
+nice -language
Match nice but not language
+nice ~language
Match nice, but mark down as less relevant rows that contain language
+nice*
Match nice, nicely, nicety, nice language, etc
"nice language"
Match the exact term "nice language"
+nice +(language country)
Match either "nice language" or "nice country"
+nice +(>language <country)
Match either "nice language" or "nice country", with rows matching "nice language" being considered more relevant
Before we take a look at how these effect queries in practical use, I'd first like to introduce the topic of relevance. Result relevant, often known as match quality or score, is a number returned by MySQL that allows you to rank how much of a query a row matched. To get relevance from a query, simply copy the full-text WHERE clause into the fields you want to select, like this:
SELECT Opinion, MATCH(Opinion) AGAINST('nice language' IN BOOLEAN MODE) AS Score FROM opinions WHERE MATCH(Opinion) AGAINST ('nice language' IN BOOLEAN MODE);
Do not worry about using MATCH() twice - the two matches are identical, and the MySQL optimizer will pick up on this and only execute it once. However, it does give us a new field, Score, to play with. So, the query above says "Select all opinions and their relevance where the row has either "nice" or "language". Here is what the above query outputs:
+---------------------------------------+-------+ | Opinionย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย | Score | +---------------------------------------+-------+ | PHP is a very nice languageย ย ย ย ย ย ย ย ย ย ย |ย ย ย ย ย 2 | | Spain is a nice country to visitย ย ย ย ย ย |ย ย ย ย ย 1 | | Perl is not as nice a language as PHP |ย ย ย ย ย 2 | +---------------------------------------+-------+
Naturally it would be best to sort by Score descending to get the matches in order of most relevant first, however note that the Score of the two fields with "nice" and language in is 2, whereas the field with just "nice" in is 1. Now we get to play with the extended boolean mode operators - for example, if we wanted to match "nice language" or "nice country", we could use this query:
SELECT Opinion, MATCH(Opinion) AGAINST('+nice +(language country)' IN BOOLEAN MODE) AS Score FROM opinions WHERE MATCH(Opinion) AGAINST ('+nice +(language country)' IN BOOLEAN MODE);
The pluses are in there to make sure that both nice and either language or country are included in the result. This time Score is 2 for all three fields, as all three match nice and all three have either language or country. We can give more relevancy information to MySQL by saying that country is more important to match than language by using >, like this:
SELECT Opinion, MATCH(Opinion) AGAINST('+nice +(language >country)' IN BOOLEAN MODE) AS Score FROM opinions WHERE MATCH(Opinion) AGAINST ('+nice +(language >country)' IN BOOLEAN MODE);
This time we get 1 for the two "nice language" matches and 1.25 for "nice country" - MySQL has increased the score for "country" by 0.25. We can push this further by decreasing the score for "language" at the same time, like this:
SELECT Opinion, MATCH(Opinion) AGAINST('+nice +(<language >country)' IN BOOLEAN MODE) AS Score FROM opinions WHERE MATCH(Opinion) AGAINST ('+nice +(<language >country)' IN BOOLEAN MODE);
The output for that query is shown below - as you can see, the nice language matches have moved down another notch.
+---------------------------------------+------------------+ | Opinionย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย |ย ย ย ย ย ย ย ย ย ย ย ย Score | +---------------------------------------+------------------+ | PHP is a very nice languageย ย ย ย ย ย ย ย ย ย ย | 0.83333337306976 | | Spain is a nice country to visitย ย ย ย ย ย |ย ย ย ย ย ย ย ย ย ย ย ย ย 1.25 | | Perl is not as nice a language as PHP | 0.83333337306976 | +---------------------------------------+------------------+
The exact numbers in the score are usually irrelevant - it is the ranking that counts. Therefore, we could have achieved the same effect by using the negation operator, ~, which would subtract points from the word language if we used this query:
SELECT Opinion, MATCH(Opinion) AGAINST('+nice ~language)' IN BOOLEAN MODE) AS Score FROM opinions WHERE MATCH(Opinion) AGAINST ('+nice ~language)' IN BOOLEAN MODE);
This time MySQL will match all rows that contain nice or language, but will "penalise" rows that contain language. This is helpful for marking known irrelevant words without specifically removing rows that contain them.ย